Home > Blockchain >  Find and replace when two special characters are together
Find and replace when two special characters are together

Time:12-11

I am trying to remove *| in a string , if *| is present multiple times I want to remove globally.

Input: C1*12.58|*|*|

enter image description here

Desired output:

C1*12.58

I tried:

ip.replaceAll("*|","")

But thats not working in mine. Is there any other option. I have searched but I couldn't find a regex to two replace that two special character together .

CodePudding user response:

When it comes to overlapping a lookahead can help.

console.log('C1*12.58|*|*|'.replace(/\|(?=\*)|\*\|/g, ''))

It either matches a | if followed by * or *| - See this demo at rege101


Or match | and repeat *| by use of a non-capturing group: \|(?:\*\|)

CodePudding user response:

If i understand correctly you want to remove every occurrence of |* meaning the output to your string would be "C1*12.58|" and not "C1*12.58"

The following regex statement produces "C1*12.58|":

let result = "C1*12.58|*|*|".replace(/\|\*/g, "");

CodePudding user response:

This can work for you. You need to escape special characters like * and |.

let result= 'C1*12.58*|*|'.replace(/\*\|/gm,"")
  • Related