I am trying to replace the same string *a*a
consistently with *a
.
Tried many variations of something like this, but none really worked:
s = s.replace( /\b*a*a\b/g, "*a");
So far running this leads to all xzy*a
being replaced with xyz
I am trying to replace the same string *a*a
consistently with *a
.
Tried many variations of something like this, but none really worked:
s = s.replace( /\b*a*a\b/g, "*a");
So far running this leads to all xzy*a
being replaced with xyz
An asterisk is a special regex character.
You just have to escape it like this: \*a
in place of *a
*
is a special regex character. If you want to match only an actual asterisk, then you have to escape it like this:
s = s.replace( /\*a\*a/g, "*a");
Working demo: http://jsfiddle.net/jfriend00/gvgshwyz/