Take the 2-minute tour ×

I want to get the variable names in javascript which define a string for that I have wrote a regular expression

var x = "sdfsfsdf";


The problem with this expression is when I am using RegExp.$2 I should get the variable name as x as we consider the above code. It works fine with some expression but if there is code like

function(a) {var b = document.createElement("script");}

then the result is function(a){var b.

Please help me change my regular expression so it works in both cases.

NOTE: javascript variables can also be declared without var i.e. x = "sdfsfsf";

share|improve this question
What about var test = 'var test = "var test = \"var test;\""'; That's a valid variable as well. –  Blender Apr 10 '12 at 5:43
sorry i didn't get you if you are specifying to add var in regular expression then it is not necessary to declare a variable with var it can also be plainly declared in that case we might miss such variables. –  user1275375 Apr 10 '12 at 5:46
I'm trying to say that regex isn't really the tool for parsing arbitrary JavaScript variable decelerations. Can you explain your intentions a little? Why do you need to do this? Maybe there's a simpler way of doing it. –  Blender Apr 10 '12 at 5:48
What about variable names that don't use word characters, such as Underscore (), dollar sign ($) and so on. You will not get the whole name and completely miss var fred = 'fred' –  RobG Apr 10 '12 at 5:58
i want to extract the variables because they are used to declare dynamic functions like s=document.createElement('script');s.src="some.js"; –  user1275375 Apr 10 '12 at 6:14
add comment

3 Answers

If your strings won't be too crazy, you can try this:



> var r = /[a-z_$][a-z0-9$_]*\s*=\s*.*?(;|$)/gi;
> 'var x = "sdfsfsdf";'.match(r);
  ["x = "sdfsfsdf";"]
> 'function(a) {var b = document.createElement("script");}'.match(r);
  ["b = document.createElement("script");"]
share|improve this answer
/[a-z_$][a-z0-9$_]+\s*=\s*.*?/gi slightly shorter. –  row1 Apr 10 '12 at 5:59
Thanks, I'll edit it in. –  Blender Apr 10 '12 at 5:59
What about other unicode characters that don't fit in that range? –  RobG Apr 10 '12 at 6:08
If your strings won't be too crazy. I'm not writing a JavaScript parser in JavaScript. If there are basic variables that are being declared, extracting them is simple. You can always find a way to break out of a regex like this. –  Blender Apr 10 '12 at 6:09
Also, here's a regex: stackoverflow.com/a/9337047/464744 –  Blender Apr 10 '12 at 6:10
add comment

(^|;|{)          at the beginning, after a semicolon or the bracket
\s*              0-n whitespace characters
(var\s+)?        could be followed by "var" with at least one whitespace
(\S+)            at least one none whitespace character
\s*=\s*          equal sign with possibly surrounded whitespaces
['"][^'"]*['"]   a 'string'
\s*(}|;|$)       have to end with the bracket or a semicolon or the end of the variable has been reached

Also see this example.

share|improve this answer
add comment

Try this regex: ([^\w]?(\w.*?)(\s*=\s*)(['"]*)(.+?)(['"]*)\1)

share|improve this answer
it is not working it is yielding the same output –  user1275375 Apr 10 '12 at 5:53
@Steven, Can you explain what's wrong with the OP's regex, and why you think yours will work where his didn't? Because, no offense intended, this looks like a wild guess to me. –  Alan Moore Apr 10 '12 at 7:24
The [^\w] in the regex means there should be a character which is not [a-zA-Z_], so it will make it not matching function(a) { this part –  Steven You Apr 10 '12 at 7:53
I don't think that would work, but it doesn't matter because the ? quantifier makes that part optional, so your regex will still match anything the original would. Aside from that addition, the regex is just as the OP wrote it, including several obvious errors. For example, what's the \1 at the end for? A backreference to the containing group is legal in some regex flavors in some circumstances, but never in JavaScript. But no amount of tweaking is going to make a regex work; the job requires a full-blown JS parser. –  Alan Moore Apr 10 '12 at 12:02
add comment

Your Answer


