Open brad4d opened 2 years ago
The matched text is not included: you need to escape |
in your regular expressions.
Doh!
nevermind sorry.
Well, it still needs to be clarified whether RegExps are allowed, right?
Wait!, I'm not crazy after all
The "include the separators" behavior is triggered by the expression including groups.
a.split(/(\|)/);
(7) ['a', '|', 'b', '|', 'c', '|', 'd']
Oh well, I didn't know about that! Then the initial question still holds completely.
Yikes. Every group is included.
const b = 'a:sep1:b:sep2:c:sep3:d:sep4:e';
b.split(/((:)(\w+)(:))/); // ['a', ':sep1:', ':', 'sep1', ':', 'b', ':sep2:', ':', 'sep2', ':', 'c', ':sep3:', ':', 'sep3', ':', 'd', ':sep4:', ':', 'sep4', ':', 'e']
This definitely breaks the goal of having split
be the opposite of join
.
Will include this. Yes, it should support regexp, just like perl does for example.
Edit: I.e not add the separators into the returned array.
I believe it depends on the method name we finally choose.
If it is the name "splitn" or "splitN" I think we should support regexp. If choose another name like "explode", supporting regexp is not a must.
Precedent: String.prototype.replaceAll
, the early version of it only support string for the best perf and the main pain point (s.replace("foo")
doesn't work as expect ) in usage, but committee require it must support regexp unless it change to some other name like "substitute" .
The current
split
method accepts aRegExp
as the first argument. In that case matched text for defined groups are included in the returned array.EDIT: This is the original example which was incorrect as pointed out below.
EDIT: This is a valid example.
Will
splitn
allowRegExp
arguments? If so, how will it treat them?