Replace a character of a string

前端 未结 2 1486
萌比男神i
萌比男神i 2021-01-21 05:40

I have a string that looks like this: [TITLE|prefix=a].

From that string, the text |prefix=a is dynamic. So it could be anything or emp

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-21 06:26

    I think the solution to your problem would look similar to this:

    let str = 'Lorem ipsum [TITLE|prefix=a] dolor [sit] amet [consectetur]';
    
    str = str.replace(/(\[[^\|\]]+)(\|[^\]]*)?\]/g, "$1$2|suffix=z]");
    
    console.log(str);
    

提交回复
热议问题