[英]Javascript: Check string to be the pattern of RegExp Object
I need to find a solution for checking a string that can be the pattern of RegExp Object like this.我需要找到一个解决方案来检查可以是 RegExp 对象模式的字符串,就像这样。
str
=> new RegExp( str
) can run and return a regex to use with test(), match(), ... str
=> new RegExp( str
) 可以运行并返回一个正则表达式以用于 test()、match()、...
How can I check str
available to use like the above way?如何像上述方式一样检查可用的
str
? (I am trying to create a component like the website regex101.com so I want to know how they can check and show errors in regular expression
input) Thank you a lot. (我正在尝试创建一个像网站 regex101.com 这样的组件,所以我想知道他们如何检查和显示
regular expression
输入中的错误)非常感谢。
Just use a try catch to test it:只需使用 try catch 来测试它:
function isValidRegex(str) {
try {
new RegExp(str);
return true;
} catch(ex) {
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.