slevithan / xregexp

Extended JavaScript regular expressions
http://xregexp.com/
MIT License
3.31k stars 278 forks source link

the split() limit parameter is not the same meaning as PCRE split() LIMIT #303

Closed alexwenbj closed 4 years ago

alexwenbj commented 4 years ago

For split(), in perl,the LIMIT means: split /PATTERN/,EXPR,LIMIT

If LIMIT is specified and positive, it represents the maximum number of fields into which the EXPR may be split; in other words, LIMIT is one greater than the maximum number of times EXPR may be split. Thus, the LIMIT value 1 means that EXPR may be split a maximum of zero times, producing a maximum of one field (namely, the entire value of EXPR) it is the same as PHP/Python: if limit is nonzero, at most limit splits occur, and the remainder of the string is returned as the final element of the list.

But in xregexp, it just means return how much elements of the split array. In PCRE mode(include Perl/PHP/Python),the limit affects the split ACTION In xregexp,the limit affects the return array elements number.

In Perl: print join(" ",split(/:/, ':a:b::c', 3)) => a b::c In xregexp:

var s = XRegExp.split(':a:b::c', /:/, 3);
console.log(s);

=>["", "a", "b"]

Ref: https://perldoc.perl.org/functions/split.html https://www.php.net/manual/en/function.preg-split.php https://docs.python.org/3/library/re.html

slevithan commented 4 years ago

That’s an interesting behavior difference. However, the behavior of XRegExp.split’s limit argument is intentional. It follows the behavior of JavaScript’s String.prototype.split. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split.

alexwenbj commented 4 years ago

So sad.Maybe it is better be compatible with PCRE.