Reputation: 2645
My input looks like this:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. _4,7,13 Nullam suscipit orci sit amet feugiat facilisis. Curabitur eget 8 ligula malesuada, vehicula 3,6 quam sit amet, _5 tempor velit.
I need to capture every number that's in a comma-separated list preceded by _
, individually and using a single regex.
In other words, I need the bolded numbers above:
[4, 7, 13, 5]
I've been trying again and again to make this work without success. I'd like to know if this is even possible before forfeiting and going with multiple expressions.
I'm looking for a solution in Javascript, but obviously any pointer will help.
Upvotes: 1
Views: 42
Reputation: 785721
You can use this code in Javascript:
var input = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. 4,7,13 Nullam suscipit orci sit amet feugiat facilisis. Curabitur eget 8 ligula malesuada, vehicula 3,6 quam sit amet, 5 tempor velit.';
var matches = [];
input.replace(/_(\d+(?:,\d+)*)\b/g, function($0, $1) {
matches = matches.concat( $1.split(/,/g) ); return $1; } );
console.log(matches);
//=> ["4", "7", "13", "5"]
Upvotes: 1
Reputation: 67988
(_\d+(?:,\d+)*)
Try this.Grab the captures.Then split by ,
.See demo.
http://regex101.com/r/rQ6mK9/23
Upvotes: 0