1

I need some help with regular expressions in JavaScript.

I have the following string.

var str = "SOme TEXT #extract1$ Some more text #extract2$ much more text #extract3$ some junk";

I want to extract all the text between # and $ into an array or object.

Final output would be:

var result = [extract1,extract2,extract3]

extract1, extract2,extract3 can contain any characters like _,@,&,*

JabberwockyDecompiler
  • 3,318
  • 2
  • 42
  • 54
CKR
  • 213
  • 4
  • 8

3 Answers3

2

Regex for you will be like #(?<extract>[\s\S]*?)\$

Named group 'extract'will contain the values you want.

As Alex mentioned, if javascript doesnt support named group, we can use numbered group. So modified regex will be #([\s\S]*?)\$ and desired values will be in group number 1.

Shekhar
  • 11,438
  • 36
  • 130
  • 186
  • 1
    nice touch with the [\s\S]. how does it perform against [^\$] ? – yoavmatchulsky Apr 28 '11 at 06:01
  • @yoavmatchulsky, certainly [^\$] is faster than [\s\S] – Shekhar Apr 28 '11 at 06:02
  • 1
    [JavaScript doesn't have named capturing groups](http://stackoverflow.com/questions/5367369/named-capturing-groups-in-javascript-regex). Why do you escape `#`? Also, why the `?` after the `*`? – alex Apr 28 '11 at 06:04
  • @alex, I dont know anything about JavaScript, I am from C# world. We can use numbered group instead of named groups. – Shekhar Apr 28 '11 at 06:05
  • 1
    @Shekhar Also, I understand the `?` now. Ungreedy. How did I forget that? – alex Apr 28 '11 at 06:09
  • 1
    @alex the `*?` means its a non-greedy `*` which means it will capture as little as possible. EG a regex of `.*b` on a string `"aaaababa"` will capture `"aaaabab"` but a non greedy `.*?b` will capture `"aaaab"`. **EDIT** beat me to it alex. ;) – James Khoury Apr 28 '11 at 06:11
  • @James Yeah, I somehow forgot (though I use it probably 5 times a week). See comment above :) – alex Apr 28 '11 at 06:13
2

You can use JavaScript Regular expressions' exec method.

var regex = /\#([^\$]+)\$/g,
    match_arr,
    result = [],
    str = "some #extract1$ more #extract2$ text #extract3$ some junk";
while ((match_arr = regex.exec(str)) != null)
{
   result.push(match_arr[1]);
}
console.log(result);

Reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec

dheerosaur
  • 14,736
  • 6
  • 30
  • 31
0
var matches = str.match(/#(.+?)\$/gi);

jsFiddle.

alex
  • 479,566
  • 201
  • 878
  • 984