传递手动创建的RegExp对象时,String.match返回不正确
String.match return incorrect when passed a manually created RegExp object?
假设我有这个regexp
(^|;)['s]*style=([^;]*)
与匹配的字符串
test=en; style=night
根据String.match 的MDN文档
- 我可以传入一个正则表达式文字-
/(^|;)['s]*style=([^;]*)/
- 我可以传递一个字符串,它被包装在RegExp对象
"(^|;)['s]*style=([^;]*)"
中 - 或者我可以传入创建的RegExp对象-
new RegExp("(^|;)['s]*style=([^;]*)")
问题是,它们的评估值并不都相同。
/(^|;)['s]*style=([^;]*)/ => array of matches
"(^|;)['s]*style=([^;]*)" => null
new RegExp("(^|;)['s]*style=([^;]*)") => null
这种情况下发生了什么?为什么他们不都对同一组比赛进行评估
顺便说一句。在Chrome 38.0.2125.101和IE 11.0.9600.17278 上测试
当使用构造函数时,正常的字符串转义规则(当包含在字符串中时,前面带有''的特殊字符)是必需的例如,以下是等效的:
var re = /'w+/;
var re = new RegExp("''w+");
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
相关文章:
- 当正则表达式与空字符串匹配时,是否可以使用 regexp.exec 循环模拟 string.match
- Java 脚本 String.match() 无法按预期工作
- 我无法准确地理解JavaScript的方法string.match(regexp)的g标志是如何工作的
- Javascript 正则表达式奇怪的行为 String.match()
- 为什么string.match(..)[0]引发异常
- 传递手动创建的RegExp对象时,String.match返回不正确
- 为IE8优化JS-String.replace和String.match
- 为什么不'当存在全局标志时,String.match()会产生预期的结果
- 为什么String.match在结果数组中返回重复项
- RegExp适用于String.match,但不适用于String.split
- RegExp的exec()函数和String的match()函数有什么区别
- 可以'我不理解javascript String.match(regexp)方法的行为
- “string.match()“;不是一个函数
- 如何在javascript中使用String.match()
- JavaScript:不能操作string.match生成的数组
- JavaScript:不能操作string.match生成的数组
- 为什么't String#match()结果不包含捕获的值?
- 为什么String.match()方法返回多个值
- Javascript:String.match()-在正则表达式中传递字符串变量
- 带未知长度通配符的string.match()