regex - Ruby match groups of JavaScript variables -


i'd extract variable names scratch of code:

var a,b,c,  foo = "test string"; 

i'd match result contain a,b,c , foo , value optionally. able split string comma wonder if there's way directly regex.

var\s+(.+)\s*,?\s*=.+; 

you can test out @ http://rubular.com/ shows me a,b,c,foo part i'd output in match groups that:

  1. a
  2. b
  3. c
  4. foo

you can add more capturing groups capture parts of string way want:

var\s+(\w+),(\w+),(\w+),\s*(\w+)\s*=.+; 

output of demo:

1.  2.  b 3.  c 4.  foo 

mind if there non-specified number of arguments, not work.

as alternative, use regex \g forces consecutive matches:

(?:var\s+|(?<!^)\g)[,\s]*(\w+) 

output of another demo:

match 1 1.  match 2 1.  b match 3 1.  c match 4 1.  foo 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -