Fyrd / caniuse

Raw browser/feature support data from caniuse.com
https://caniuse.com
Creative Commons Attribution 4.0 International
5.64k stars 1.38k forks source link

String.prototype.split() with a regex with grouping ( parentheses ). #3467

Open BobStein opened 7 years ago

BobStein commented 7 years ago

Splitting a string normally leaves out the delimiters.

> "a and b or c".split(/and|or/)
["a ", " b ", " c"]

But grouping-parentheses in a regular expression preserves them in the odd-numbered indexes of the output.

> "a and b or c".split(/(and|or)/)
["a ", "and", " b ", "or", " c"]

This was not supported in IE before version 11. And MDN says "not all browsers support this capability." I'd like to see whether other browsers support this feature. But the current information in caniuse.com does not address this.

These methods are supported in effectively all browsers (since IE6+, Firefox 2+, Chrome 1+ etc)

GPHemsley commented 2 years ago

It seems split() support data is now provided by MDN compat data: https://caniuse.com/mdn-javascript_builtins_string_split https://caniuse.com/mdn-javascript_builtins_symbol_split https://caniuse.com/mdn-javascript_builtins_regexp_--split

These are all interrelated concepts with different support histories, but none of them explicitly mention splitting with groupings as a separate caveat. In fact, even the aforementioned MDN page has removed the aforementioned statement about (lack of) support.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/split https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@split

It might be worth following up at the MDN compat repo on this topic.

See also: