[SOLVED] How to remove a varying part from a string?


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?


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")

Answered By – Ryan O'D

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *