I keep getting the following error
Uncaught SyntaxError: Invalid regular expression: /$_POST['/: Unterminated character class
I searched a few threads and they all pointed me to the fact the at I am not properly escaping the string. I loaded a php file into a javascript var as a string , then I search through it for the following, but I have tried escaping to no success.
according to List of all characters that should be escaped before put in to RegEx?
These are all the characters that need escaping I believe.
. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
and here are the code snipets that are failing me. (This is the clean code everything needs escaping)
var n = data.search("$_POST['");
n = res.search("'];");
n = data.search("//hurdle{");
n = res.search("}");
If someone could show me how to properly do this, it would be very appreciated. Thank you
-
How are you attempting to escape these characters? With a \?Steve Bennett– Steve Bennett2014年07月29日 02:50:30 +00:00Commented Jul 29, 2014 at 2:50
2 Answers 2
You need to escape the dollar and brackets: \$, \[ and \]
For example:
arrayOfMatches = yourString.match(/\$_POST\['/g);
4 Comments
g is for getting all the matches in one go. That's just one example, there are a number of JS regex methods, e.g. exec. Here is a decent summary For first expression
data.search(/\$_POST\['/)