Javascript regex to match string if only preceded by backslash
JavaScript definitely doesn't support lookbehind assertions. The next best way to get what you want in my opinion would be
(?:^|[^\\])(U[0-9]{4})
Explanation:
(?: # non-capturing group - if it matches, we don't want to keep it
^ # either match the beginning of the string
| # or
[^\\] # match any character except for a backslash
) # end of non-capturing group
(U\d{4}) # capturing group number 1: Match U+4 digits
source to share
Unfortunately JS doesn't seem to support the appropriate syntax for this, i.e. approval back /(?<!\\)U[0-9]{4}/
.
So, you need to use:
/[^\\]U[0-9]{4}/
This is the syntax for the regexp literal. If you put the regex in a string, you'll have to strip the backslash again:
"[^\\\\]U[0-9]{4}"
source to share
I would suggest using lookbehind, but JavaScript doesn't seem to support it . Perhaps you can match U [0-9] {4}, find where the match is, and check the character to the left of it to see if it's a \ or not?
source to share
JavaScript RegExp does not support negative appearance assertions. Ideas suggesting you only match / [^ \] U / will match strings like "_U", so not an answer. Your best bet is to use two regular expressions, the first to find all occurrences and then the second to filter out the look.
"\\U0000 U0000".match(/\\?U[0-9]{4}/g)
.filter(function (match) {
return !/^\\/.test(match)
})
source to share