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

1

1 Answer 1

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");

Sign up to request clarification or add additional context in comments.

2 Comments

Lookbehind is not supported in javascript.
Thank you. I wasn't aware of that. I edited my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.