underscore.string
underscore.string copied to clipboard
words' regex argument crashes with quantifiers
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.
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+/);
- As trim adds the quantifiers automatically (+) then delimiter parameter should avoid it
- To get the expected result, on the split call have to be added the + quantifier.
See this fiddle: http://jsfiddle.net/mauvega/ymupb7rd/