Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is ^|\s+ used instead of just \s+ in .split()? #18

Closed
ChALkeR opened this issue Jul 19, 2018 · 5 comments
Closed

Why is ^|\s+ used instead of just \s+ in .split()? #18

ChALkeR opened this issue Jul 19, 2018 · 5 comments

Comments

@ChALkeR
Copy link

ChALkeR commented Jul 19, 2018

On the following lines, ^|\s+ is used:

function parseTypenames(typenames, types) {
return typenames.trim().split(/^|\s+/).map(function(t) {
var name = "", i = t.indexOf(".");

Does that do anything different from what just \s+ would do?

It causes problems when trying to use this library in QML environment (V4 JS engine from Qt).
Ref: QTBUG-62471.

@ChALkeR ChALkeR changed the title Why is /^|\s+/ used instead of just ` in .split()? Jul 19, 2018
@mbostock
Copy link
Member

I think the only difference is when typenames is the empty string or entirely whitespace.

"".split(/^|\s+/) // []
"".split(/\s+/) // [""]
@ChALkeR
Copy link
Author

ChALkeR commented Jul 20, 2018

@mbostock Ah, thanks, I totally missed that.

Perhaps that could be solved with .filter(function(t) { return t; }) instead?
Or, a length check after .trim().

@ChALkeR
Copy link
Author

ChALkeR commented Jul 20, 2018

I'm not even sure though that this would make things work under Qt QML, I checked pretty long ago.
Perhaps that needs a recheck before anything is done here.

@mbostock
Copy link
Member

mbostock commented Aug 2, 2019

I’m not planning on taking any action. Thank you for the question.

@mbostock mbostock closed this as completed Aug 2, 2019
@localpcguy
Copy link

localpcguy commented Mar 26, 2020

I think I've found a (browser?) bug related to this regex and the question asked by @ChALkeR here. In Internet Explorer (v11 is where I saw this), the regex /^|\s+/ splits a string into it's individual characters. So something like "end".trim().split(/^|\s+/) in IE11 outputs ["e", "n", "d"]. In modern browsers, it outputs [ "end" ] as expected.

The issues seems to be the pipe, it looks like it causes IE to split not on the whitespace but on nothing, effectively.

I'm not sure what the expected browser support is, but as suggested above, using .split(/\s+/).filter(function(t) { return t; }) would achieve the same result, work in IE9+ (when .filter was added), and eliminate the regex bug.

Let me know if this should be a new bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants