The Coding Wombat The Coding Wombat - 1 year ago 87
Javascript Question

Match pattern not preceded by character

I want to make my regex match a pattern only if it is not preceded by a character, the

(circumflex) in my case.

My regex:

Text to test it on:
Test: ^Anotherword

Matches: "Test" and " Anotherword", even though the latter is preceded by a circumflex. Which I was trying to prevent by inserting the
at the start. So I'm not only trying to not match the circumflex, but also the word that comes after it. " Anotherword" should not be matched.

- This is what should stop the regex from matching if an accent circonflexe is in front of it.

- Match any word that is not preceded by a circumflex.

I cannot use lookbehind because of JavaScript limitations.

sln sln
Answer Source

Use ([^^\w]|^)\w+

It basically injects a word boundary while excluding the ^ as well.
[^\w] = \W\b\w

Otherwise [^^] will match a '^T'
and \w+ will match est.

You can see it if you put capture groups around it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download