3

I need to grab the value of a variale found in a javascript file. Using xmlhttprequest I can get the page's source, however, I need a regular expression to grab the value of a few variables located within the file.

I want to grab the value of a variable:

var VARIABLE_NAME = VALUE

I tried this regular expression:

/var VARIABLE_NAME \=/

But I think I am missing what needs to be done for getting what's after the equal sign.

puk
  • 16,318
  • 29
  • 119
  • 199
Joshua Redfield
  • 2,217
  • 2
  • 14
  • 10

2 Answers2

4

Try:


regex = /VARIABLE_NAME=(.*)/;
alert(yourText.match(regex)[1]);

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1

You don't need to add a \ before the = sign. You may add also some \s* in your Regexp if you're not sure of the number of spaces.

You can see here how to capture a group : How do you access the matched groups in a JavaScript regular expression?

Community
  • 1
  • 1
Baldrick
  • 23,882
  • 6
  • 74
  • 79