If I try this code in firefox it works fine
var words = String.split(new RegExp(/[\-\s]/));
words // ["/[\-\s]/"]
The same code in IE not!
var words = String.split(new RegExp(/[\-\s]/));
words "Object doesn't support property or method 'split'"
Why? and what is the best way to fix it in IE?
Update:
The problem is that your argument is called
string(all lower case), but you’re usingString(with an initial capital) when you’re trying to split it. JavaScript is a case-sensitive language,string !== String.So change this:
to this:
Original answer:
splitis a function on theString.prototype(effectively, on instances of strings), not onStringitself (the constructor function).So:
Side note: You don’t have to wrap a regular expression literal (
/[\-\s]/) innew RegExp(...)unless you’re working around an oldbugissue in some implementations around the global flag and caching/reuse of local literals across function calls, which isn’t relevant tosplitas you don’t use thegflag with it.