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

Community
  • 1
  • 1
Konstantino Sparakis
  • 2,862
  • 3
  • 18
  • 30

2 Answers2

2

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

For example:

arrayOfMatches = yourString.match(/\$_POST\['/g);
zx81
  • 41,100
  • 9
  • 89
  • 105
0

For first expression

data.search(/\$_POST\['/)
Mritunjay
  • 25,338
  • 7
  • 55
  • 68