How To Check For Odd Numbers Of Backslashes In A Regex Using Javascript?
I have recently asked a question regarding an error I have been getting using a RegExp constructor in Javascript with lookbehind assertion. What I want to do it, to check for a num
Solution 1:
JS doesn't support lookbehinds (at least not all major browsers do), hence the error. You could try:
(?:^|[^\\\n])\\(?:\\{2})*(?![0-4]\b)\d+
Or if you care about decimal numbers:
(?:^|[^\\\n])\\(?:\\{2})*(?![0-4](?:\.\d*)?\b)\d+(?:\.\d*)?
Note: You don't need \n
if you don't have multi line text.
Regex breakdown:
(?:
Beginning of non-capturing group^
Start of line|
Or[^\\\n]
Match nothing but a backslash
)
End of non-capturing group\\(?:\\{2})*
Match a backslash following even number of it(?![0-4](?:\.\d*)?\b)
Following number shouldn't be less than 5 (care about decimal numbers)\d+(?:\.\d*)?
Match a number
JS code:
var str = `\\5
\\\\5
\\\\\\5
\\\\\\4
\\4.
\\\\\\6
`;
console.log(
str.match(/(?:^|[^\\\n])\\(?:\\{2})*(?![0-4](?:\.\d*)?\b)\d+(?:\.\d*)?/gm)
)
Post a Comment for "How To Check For Odd Numbers Of Backslashes In A Regex Using Javascript?"