"can't supply flags when constructing one RegExp from another"

前端之家收集整理的这篇文章主要介绍了"can't supply flags when constructing one RegExp from another"前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在Chrome47下遇到这个错误,但是在版本54下是正常的.

var re = /^\w{8}$/;
var reObj = new RegExp(re,'i');

解决方法也很简单,就是把i标志去掉就可以了,但是这样并不是我的想法,我就是想加上i标志.

这是为什么呢?
后来在MDN上找到这句话:

Starting with ECMAScript 6,new RegExp(/ab+c/,‘i’) no longer throws a TypeError (“can’t supply flags when constructing one RegExp from another”) when the first argument is a RegExp and the second flags argument is present. A new RegExp from the arguments is created instead.

也就是说,从ES6开始,new RegExp(re,'i')这种写法不会再抛出"can't supply flags when constructing one RegExp from another"这个错误,更详细的说明可以查看这里RegExp.

原文链接:https://www.f2er.com/regex/358772.html

猜你在找的正则表达式相关文章