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:
- a
- b
- c
- 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
Post a Comment