I am writing a rudimentary lexer using regular expressions in JavaScript, and I have two regular expressions (one for single quotes and one for strings with double quotes) that I want to combine into one. These are my two regular expressions (I added characters ^and $for testing purposes):
var singleQuotedString = /^'(?:[^'\\]|\\'|\\\\|\\\/|\\b|\\f|\\n|\\r|\\t|\\u[0-9A-F]{4})*'$/gi;
var doubleQuotedString = /^"(?:[^"\\]|\\"|\\\\|\\\/|\\b|\\f|\\n|\\r|\\t|\\u[0-9A-F]{4})*"$/gi;
Now I tried to combine them into one regular expression as follows:
var string = /^(["'])(?:[^\1\\]|\\\1|\\\\|\\\/|\\b|\\f|\\n|\\r|\\t|\\u[0-9A-F]{4})*\1$/gi;
However, when I test the input "Hello"World!", it returns trueinstead false:
alert(string.test('"Hello"World!"'));
I realized that the problem is in [^\1\\]which must match any character except \1(which is either a single or double quote - a line separator) and \\(which is a backslash character).
The regular expression correctly filters the backslash and matches the delimiters, but does not filter the delimiter inside the string. Any help would be appreciated. Please note that I referenced the Crockford railway diagrams to write regular expressions.
source
share