no-invalid-regexp

禁止在 RegExp 构造函数中使用无效的正则表达式字符串

配置文件 中的 "extends": "eslint:recommended" 属性启用了该规则

解析代码时,正则表达式字面中的无效模式是 SyntaxError,但 RegExp 构造函数中的无效字符串仅在执行代码时才会抛出 SyntaxError

规则详情

此规则不允许在 RegExp 构造函数中使用无效的正则表达式字符串。

此规则的错误代码示例:

/*eslint no-invalid-regexp: "error"*/

RegExp('[')

RegExp('.', 'z')

new RegExp('\\')

DzO9VpSFHJiTjGCgt1KSooNhAIn1o64th84QTOygwWSy7fBgj4TGxtI1vaW0J9Ta

/*eslint no-invalid-regexp: "error"*/

RegExp('.')

new RegExp

this.RegExp('[')

VYqu9fFJzVQPOl/mI5ztGpsPJU9wtuV3V/EIqvP2sEdTNAXxVzuBPXEp+hB7bN5TLjs+yQSodprUggKaEWb77HMRsrbuPACmvKE8herezhe+gSew7ryTX/xydJ4rFdhOLDHyvevXiN344NCj9LPg/DgQmcrhOQPe0Iu9RBv0t/k=

4ZGi80FK7bPV0QAJuJIoP9iNyAbhQoIVlWa+76qcKdOt0LMNFJUvutKHauTbIw4RmZyS9eOVX1CEUgcqbDBLa693ukYFh+P7B/noqXY8J7NQtylLft9YKGkNdGQ4fF7llCnu0buGzCX/KoGMAuJcGUggrQRxt0BH4jqq5Y/xnq3ea5YoM5RruCihujZDXLgm5HuDHfMppfn1SkeXd5hL+F8vjuK3ZknfIjZ+deqeaWNMLphlztvaZ/JclyROimey

选项

gOiAZnAcPh/ECcp3u5icNGWme6rjSAlfrlhB9v101a7QrAV0BJe1RxhVYdFDUR6U

    Ipso+1ePkiZw8zYVgYsm1FDYh8fKx7SbNxtyJLAwlJLL5szr1PVCp0dIb9RD+oT5sM1pmAyHPHKzTwssPtN/xvsx+hiJ+YlKERyLrNSt5mQ=

allowConstructorFlags

yqV+SV60QvsOsYXkYvZsZWUg7GboyCSYN0KUJXngzBvunXCRx6lGjDhaZ4zpT2t8+VfL2KV6/8XLD8n7iaxK2xmUiUuZeh2EucvGVH3HZTk9AzIcIdrmudaS0H7DQuom5QF1Eeecu93hVuvwGi5q8w==

/*eslint no-invalid-regexp: ["error", { "allowConstructorFlags": ["a", "z"] }]*/

new RegExp('.', 'a')

new RegExp('.', 'az')