Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript won't split using regex

Since I started writing this question, I think I figured out the answers to every question I had, but I thought I'd post anyway, as it might be useful to others and more clarification might be helpful.

I was trying to use a regular expression with lookahead with the javascript function split. For some reason it was not splitting the string even though it finds a match when I call match. I originally thought the problem was from using lookahead in my regular expression. Here is a simplified example:

Doesn't work:

"aaaaBaaaa".split("(?=B).");

Works:

"aaaaBaaaa".match("(?=B).");

It appears the problem was that in the split example, the passed string wasn't being interpreted as a regular expression. Using forward slashes instead of quotes seems to fix the problem.

"aaaaBaaaa".split(/(?=B)./);

I confirmed my theory with the following silly looking example:

"aaaaaaaa(?=B).aaaaaaa".split("(?=B).");

Does anyone else think it's strange that the match function assumes you have a regular expression while the split function does not?

like image 960
user45743 Avatar asked May 01 '09 16:05

user45743


People also ask

Can you split by regex Javascript?

To split a string by a regular expression, pass a regex as a parameter to the split() method, e.g. str. split(/[,. \s]/) . The split method takes a string or regular expression and splits the string based on the provided separator, into an array of substrings.

Can you split with regex?

Split by regex: re. If you want to split a string that matches a regular expression (regex) instead of perfect match, use the split() of the re module. In re. split() , specify the regex pattern in the first parameter and the target character string in the second parameter.

Is split faster than regex?

split is faster, but complex separators which might involve look ahead, Regex is only option.

How does regex split work?

The split() function returns a list of substrings split by the matches of the pattern in the string. If the pattern contains one or more capturing groups, the split() function will return the text of all groups as elements of the resulting list.


2 Answers

String.split accepts either a string or regular expression as its first parameter. The String.match method only accepts a regular expression.

I'd imagine that String.match will try and work with whatever is passed; so if you pass a string it will interpret it as a regular expression. The String.split method doesn't have the luxury of doing this because it can accept regular expressions AND strings; in this case it would be foolish to second-guess.


Edit: (From: "JavaScript: The Definitive Guide")

String.match requires a regular expression to work with. The passed argument needs to be a RegExp object that specifies the pattern to be matched. If this argument is not a RegExp, it is first converted to one by passing it to the RegExp() constructor.

like image 104
James Avatar answered Oct 01 '22 20:10

James


If I recall correctly (and I could be very wrong here), the split method was implemented in javascript before the regex engine was in wide use, so it's presumably for backward compatibility.

like image 24
EvanK Avatar answered Oct 01 '22 19:10

EvanK