Javascript won't split using regex

前端 未结 2 1263
予麋鹿
予麋鹿 2020-12-06 10:01

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 cl

2条回答
  •  猫巷女王i
    2020-12-06 10:34

    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.

提交回复
热议问题