Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  74] [ 5]  / answers: 1 / hits: 129800  / 13 Years ago, sat, october 8, 2011, 12:00:00

I have this code:



var r = /(?:^s*([^s]*)s*)(?:,s*([^s]*)s*){0,}$/
var s = a , b , c
var m = s.match(r)
m => [ a , b , c , a, c]


Looks like the whole string has been matched, but where has b gone? I would rather expect to get:



[   a   ,  b  , c , a, b, c]


so that I can do m.shift() with a result like s.split(',') but also with whitespaces removed.



Do I have a mistake in the regexp or do I misunderstand String.prototype.match?


More From » regex

 Answers
15

so finally i went with /(?=S)[^,]+?(?=s*(,|$))/g, which provides exactly what i need: all sentences split by ',' without surrounding spaces.



'       a,    OMG     abc b a b, d o WTF        foo     '.
match( /(?=S)[^,]+?(?=s*(,|$))/g )
=> [a, OMG abc b a b, d o WTF foo]


many thanks!


[#89730] Thursday, October 6, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
louiemarvinm

Total Points: 473
Total Questions: 103
Total Answers: 94

Location: Samoa
Member since Mon, Nov 8, 2021
3 Years ago
;