Home > OS >  How to remove a varying part from a string?
How to remove a varying part from a string?

Time:03-20

let: string str = 
"a=<random text>
 a=pattern:<random text (may be fixed length)>
 a=<random text>";

Suppose, from above string a= and pattern are fixed. There may or may not be \n after each line. How to remove a=pattern:<random> from the str string?

CodePudding user response:

You could use regex capturing groups. Capture the text before and after the text you want to remove.

let newStr = str.replace(/(a=. (\n)?)(a=pattern:. )((\n)?a=. )/, "$1$4")
  • Related