Вопрос

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)

Это было полезно?

Решение

(?!\\)\\

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top