Javascript动态正则表达式生成 [英] Javascript Dynamic Regex Generation
本文介绍了Javascript动态正则表达式生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望以一种方式使我的字母数字正则表达式动态,它允许用户使用允许的特殊字符作为参数。以下是我的代码...我在这里得到引号错误....任何机构都可以告诉我该如何去做?
I want to make my alphanumeric regex dynamic in a way that it takes the allowed special characters with it as an argument by the user. Following is my code ... I am getting quotes error here .... any body can tell me how to go about it ?
function aplhanumeric(value,allowed){
///^[a-z0-9_\-]+$/i
alert(allowed);
if(allowed != ''){
var regex = new RegExp('/^[a-z0-9_\' + allowed + ']+$/i');
return (value.match(regex));
}else{
return (alphaNumericRegex.test(value));
}
}
推荐答案
你实际上已经逃脱了报价,所以你必须逃避逃避
You've actually escaped the quote, so you have to escape the escape
var regex = new RegExp("^[a-z0-9_\\" + allowed + "]+$", "i");
这篇关于Javascript动态正则表达式生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文