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.

2 Answers 2

4

Try:


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

Sign up to request clarification or add additional context in comments.

1 Comment

That worked, what's the best way to remove the semi-colon at the end?
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?

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.