jQuery 核心样式指南之一指出:
All RegExp operations should be done using .test() and .exec(). "string".match() is no longer used.
为什么match()
不受欢迎?
最佳答案
因为 .exec
只是 .match
的更好版本
让我们看看
var ret1 = string.match(regexp);
var ret2 = regexp.exec(string);
如果 regexp 是非全局正则表达式,则返回值相同。
但是,如果 regexp 是全局正则表达式 (/foo/g
),则 match 仅返回匹配数组,仅此而已。不会返回像 exec 这样的额外元数据!
关于javascript - jQuery 代码风格指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7938558/