Domanda

I want to detect \( but not \\( in javascript

Also I want to replace it with ~~\(

What is wrong with the following code replace(/(?!\\)\\\(/g, '~~\\\(') ?

(This is mathjax latex)

È stato utile?

Soluzione

(?!\\)\\

Is a bit of a contradiction. "The next character must not be a \, then match a \ as the next character."

Since JavaScript doesn't currently support look-behinds, you can capture a negative character group and just put it back into the string:

replace(/(^|[^\\])\\\(/g, '$1~~\\(')

The ^| allows it to also match at the beginning of the string since the [^\\] alone would require a leading character.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top