Javascript regex to match string if only preceded by backslash

How do I match U1234 but not \ U1234 in Javascript?

I can't figure out how not to match a single backslash. The closest I can get is:

\[\\]{0}U[0-9]{4}\b

      

But it doesn't work. Any suggestions?

+2


source to share


6 answers


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

      

+9


source


[^\\]U[0-9]{4}

or something like that. It will not match the sequence at the very beginning of the subject line ...



+1


source


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}"

      

0


source


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?

0


source


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)
})

      

0


source


Ummm ... Is \ ^ U [0-9] {4} \ b for you?

0


source







All Articles