1

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

1
  • How are you attempting to escape these characters? With a \? Commented Jul 29, 2014 at 2:50

2 Answers 2

2

You need to escape the dollar and brackets: \$, \[ and \]

For example:

arrayOfMatches = yourString.match(/\$_POST\['/g);
Sign up to request clarification or add additional context in comments.

4 Comments

How about the ' , and the $? I try escaping those as well but I still get errors.
The 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
Thank you! now I'm running into a range error, haha the love of debugging.
edit: I will add the code to the main section in a second I am not sure what it is.
0

For first expression

data.search(/\$_POST\['/)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.