0

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

var x = "sdfsfsdf";

((\w.*?)(\s*=\s*)(['"]*)(.+?)(['"]*)\1)

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";

Blender
  • 289,723
  • 53
  • 439
  • 496
user1275375
  • 1,361
  • 5
  • 23
  • 38
  • 2
    What about `var test = 'var test = "var test = \"var test;\""';` That's a valid variable as well. – Blender Apr 10 '12 at 05: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 05:46
  • 1
    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 05:48
  • 1
    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 05: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 06:14

3 Answers3

0

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

/[a-z_$][a-z0-9$_]*\s*=\s*.*?(;|$)/gi

Tests:

> var r = /[a-z_$][a-z0-9$_]*\s*=\s*.*?(;|$)/gi;
  undefined
> 'var x = "sdfsfsdf";'.match(r);
  ["x = "sdfsfsdf";"]
> 'function(a) {var b = document.createElement("script");}'.match(r);
  ["b = document.createElement("script");"]
Blender
  • 289,723
  • 53
  • 439
  • 496
  • `/[a-z_$][a-z0-9$_]+\s*=\s*.*?/gi` slightly shorter. – row1 Apr 10 '12 at 05:59
  • What about other unicode characters that don't fit in that range? – RobG Apr 10 '12 at 06: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 06:09
0
/(^|;|{)\s*(var\s+)?(\S+)\s*=\s*['"][^'"]*['"]\s*(}|;|$)/i

(^|;|{)          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.

scessor
  • 15,995
  • 4
  • 43
  • 54
-1

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

Steven You
  • 439
  • 5
  • 13
  • @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 07: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 07: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