I\'m having trouble getting this Javascript Regular Expression to work.
What i want is to find a character that starts with @\" has any characters in the middle
Gumbo's regexp is very nice and all, however it fails to detect escaped quotes (e.g. \", \\\", etc.). A regexp that solves this is as follows:
/@(["'])[^]*?[^\\](?:\\\\)*\1|@""|@''/g
An explanation (continuing from Gumbo's explanation):
[^\\] matches the nearest character preccedding the ending quote that is not a backslash (to anchor the back-slash count check).(?:\\\\)* matches only if the number of backslashes is a multiple of 2 (including zero) so that escaped backslashes are not counted.|@"" checks to see if there is an empty double quote because [^\\] requires at least one character present in the string for it to work.|@'' checks to see if there is an empty single quote.The regex would be:
var regex = /@"[^"]*"/g;
Try this regular expression:
/@(["'])[^]*?\1/g
An explanation:
@(["']) matches either @" or @'[^]*? matches any arbitrary character ([^] contains all characters in opposite to . that doesn’t contain line-breaks), but in a non-greedy manner\1 matches the same character as matches with (["'])Using the literal RegExp syntax /…/ is more convenient. Note that this doesn’t escape sequences like \" into account.