esamattis / underscore.string

String manipulation helpers for javascript
http://esamattis.github.com/underscore.string/
3.37k stars 376 forks source link

words' regex argument crashes with quantifiers #194

Open ejoubaud opened 11 years ago

ejoubaud commented 11 years ago

As soon as I add a quantifier in words' pattern argument, I get an error I wouldn't with the original JS method split:

var a = _('1 2,  3').words(/, +/);
// => SyntaxError: Invalid regular expression: /^, ++|, ++$/: Nothing to repeat
var b = _('1 2,  3').words(/, */);
// => SyntaxError: Invalid regular expression: /^, *+|, *+$/: Nothing to repeat
var c = _.words('1 2,  3', /, */)
// => SyntaxError: Invalid regular expression: /^, *+|, *+$/: Nothing to repeat

See this jsfiddle: http://jsfiddle.net/ejoubaud/J9gn9/

Unrelated: Why use the obscure name words instead of the basic JS/Ruby well-known split ? I'd strongly +1 an alias.

Apart from that awesome job with this lib guys, thank you very much.

mvega-bc commented 8 years ago

I have same problem:

var array = s.words(" A01 ,  'B02' ;  'C03' ", /\W+/); 
// throws SyntaxError: Invalid regular expression: /^W++|W++$/: Nothing to repeat

I fixed it with following code: (is not general fix, but give an idea on how should be fixed)

if (isBlank(str)) return [];
return = trim(str, /\W/).split(/\W+/);

See this fiddle: http://jsfiddle.net/mauvega/ymupb7rd/