1

I'm trying to replace some string using pattern but I have no idea how to check if there is dot before string. It should be negative for .some and positive for some

var a = "some.string is replaced  and .some.string5 is not"
a.replace(new RegExp("some", "g"), "replaced")

It should give result replaced.string is replaced and .some.string5 is not THX

Pablo Matias Gomez
  • 6,614
  • 7
  • 38
  • 72
User_111
  • 49
  • 7
  • Possible duplicate of [javascript regex - look behind alternative?](http://stackoverflow.com/questions/7376238/javascript-regex-look-behind-alternative) – Martin Cup Dec 12 '16 at 18:42

1 Answers1

2

As Javascript hasn't lookbehinds implemented you can match it normally and replace the preceding character with itself with a backreference: a = a.replace(new RegExp("([^\.]|^)some\.string", "g"), "$1replaced");

Martin Cup
  • 2,399
  • 1
  • 21
  • 32