0

For instance,

var str = '<a href="one">foo</a>   <a href="two">bar</a> ...';
var reg = /href\="([^"]*)"/g;

console.log(str.match(reg));

will output [ 'href="one"', 'href="two"' ]

but i want [ 'one', 'two' ] (only the value between the parenthesis in the reg).

Is it possible directly from a function in JavaScript without writing tone of codes ?

vdegenne
  • 12,272
  • 14
  • 80
  • 106
  • 1
    Just use the regex in a loop calling `RegExp#exec(s)` and access the `match[1]` values. There is no method that will allow to get capture group contents as a list in JS. – Wiktor Stribiżew Dec 21 '16 at 14:40
  • didn't realize the function had an iterative-behavior, thanks. – vdegenne Dec 21 '16 at 14:48
  • 1
    As a hack, you can actually use `match` with the regex and then post-process the items within `map` (since the length of `href="` and `"` at the end is known). – Wiktor Stribiżew Dec 21 '16 at 14:50
  • @WiktorStribiżew yes i just thought about that but the marked answer is great – vdegenne Dec 21 '16 at 14:52

0 Answers0