the split() limit parameter is not the same meaning as PCRE split() LIMIT
alexwenbj opened this issue · 2 comments
For split(), in perl,the LIMIT means:
split /PATTERN/,EXPR,LIMIT
If
LIMITis specified and positive, it represents the maximum number of fields into which theEXPRmay be split; in other words,LIMITis one greater than the maximum number of timesEXPRmay be split. Thus, theLIMITvalue 1 means thatEXPRmay be split a maximum of zero times, producing a maximum of one field (namely, the entire value ofEXPR)
it is the same as PHP/Python: iflimitis nonzero, at mostlimitsplits 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
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.
So sad.Maybe it is better be compatible with PCRE.