Javascript won't split using regex

前端 未结 2 1253
予麋鹿
予麋鹿 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条回答
  • 2020-12-06 10:18

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题