0

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

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
montauk
  • 1
  • 2

2 Answers2

0

An asterisk is a special regex character.

You just have to escape it like this: \*a in place of *a

abarisone
  • 3,707
  • 11
  • 35
  • 54
0

* 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/

jfriend00
  • 683,504
  • 96
  • 985
  • 979