如何在Javascript中匹配U1234,而不是\ U1234?
我无法弄清楚如何不匹配单反斜杠.我能得到的最接近的是:
\[\\]{0}U[0-9]{4}\b
Run Code Online (Sandbox Code Playgroud)
但这不起作用.有什么建议?
JavaScript肯定不支持lookbehind断言.在我看来,获得你想要的东西的下一个最佳方式是
(?:^|[^\\])(U[0-9]{4})
Run Code Online (Sandbox Code Playgroud)
说明:
(?: # 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
Run Code Online (Sandbox Code Playgroud)