0

I am trying to split an expression on some specific chars. I know we can use String.split() with regex, so this was my first guess:

function expressionSplit([input]) {
    let regex = /([ (),;.]+)/g;
    let arr = input.split(regex);

    arr.forEach(item => console.log('item: ' + item));
}

expressionSplit(['let sum = 1 + 2;if(sum > 2){\tconsole.log(sum);}']);

Now this is nowhere near what I expected, so I did some more reading and found that people, unlike me, use split() with regex without problems. Puzzled, I tried this:

function expressionSplit([input]) {
    let regex = /([ (),;.]+)/g;
    let arr = input.replace(regex, '|').split('|');

    arr.forEach(item => console.log('item: ' + item));
}

expressionSplit(['let sum = 1 + 2;if(sum > 2){\tconsole.log(sum);}']);

And contrary to my expectations - it worked, mostly. Why does this happen? I expect it's some sort of JS-typical oddness, because it simply makes no sense to me, plus, as I said - other people seem to use split() with regex without problem. Also How can I split by '\t' (tab). Adding '\t' to regex seems to do nothing and '\\t' only matches 't'. Thanks.

1 Answer 1

1

There's no "JS-typical oddness" going on here -- this is all documented behavior. If you want to complain about JS oddness, you're a little late to the party...that went out of style years ago as JavaScript "grew up."

From the documentation of String#split:

If separator is a regular expression that contains capturing parentheses, then each time separator is matched, the results (including any undefined results) of the capturing parentheses are spliced into the output array. However, not all browsers support this capability.

Because you're using a grouping operator, you're getting the splitting tokens in your result as well as the content being split. If you remove the splitting tokens, it behaves as you originally expected it to:

// old:     /([ (),;.]+)/g;
let regex = /[ (),;.]+/g;
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer - it works. I didn't complain and I didn't mean anything aggressive towards JS, so you don't have to go on the defensive :D. But you cannot deny that being as flexible as JS is - it has some very interesting, if not odd features, compared to your standard programming language.
Sorry to be defensive. JavaScript has been a punching bag for a long time, and I'm just a little over it. I disagree that JavaScript has disproportionately more "quirks" than other languages; every language has it's weird, institutionalized idiosyncrasies
Full disclosure: I used to be a "hater" too. But that was before I really knew the language. If JavaScript has erred on the side of backwards compatibility (i'm looking at you, typeof), can you really blame it, given it's purpose and history?
As a counter example, I offer Python, which could have (probably should have) taken a page from JavaScript's reluctance to fix quirks in the name of not breaking everyone's code. The way Python 3 was rolled out has really hurt the language, the community, and it's adoption. I'm not saying one way is right and one way is wrong, but every change to a language is a compromise
I have had very little experience with python, so I don't know, but I can point out a couple of things that seem weird in JS - you have access to build in resources and thus you can delete for example the whole Math object. Another subtle difference is the way logical operators work, compared to compilation languages as c# and Java. I could list others, but I need to look them up :D. Can't remember all. Al in all - the more I use it - the more I like it. I quite enjoy it actually.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.