19

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?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
user45743
  • 449
  • 1
  • 6
  • 8
  • I've always used the /regex/ (no quotes) syntax for everything: match, split, replace. I didn't realize you didn't have to in some cases. – Mark Biek May 01 '09 at 16:40
  • 2
    Actually it makes sense to let match() assume a regex (what *else* would there be that you can match against?) and split() assume a string, since splitting on a string is probably faster than splitting on a regex. Only if you pass an actual regex to split(), it will do the slower regex splitting. – Tomalak May 01 '09 at 17:01

2 Answers2

31

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.

James
  • 109,676
  • 31
  • 162
  • 175
3

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.

EvanK
  • 1,052
  • 1
  • 10
  • 23
  • 1
    I think this holds; String.split was introduced in 1.1 and Regular Expressions in 1.2. Reading the docs about `split()` (here: http://wp.netscape.com/eng/mozilla/3.0/handbook/javascript/newfunc.htm), and the regex enhancements to `split()` here: http://web.archive.org/web/20010408055419/developer.netscape.com/docs/manuals/communicator/jsguide/js1_2.htm (no direct link, look under 'Regular Expressions'->'String methods' – Steen Nov 12 '13 at 11:46