如何将字符串regex exp转换为正确的regex exp [英] How do I convert string regex exp to proper regex exp

查看:51
本文介绍了如何将字符串regex exp转换为正确的regex exp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下js代码来测试电子邮件



函数testMatch(){

var current = $(document).find ('#txtbox');

var ccount = 0;

var vcode =/ ^ [a-z0-9 \ _ \-\。] + [\ @] {1} [a-z0-9] + [\。] {1} [az] {2,} / i/ *此值实际上来自json请求* /

var value =useremail@gmail.com;

if(value!= undefined&& value!= null&& value!=''){

var res = value.match(vcode);

if(res == null){

$(当前).addClass('cm -dataform-errorrow');

ccount ++;

} else {

$(当前).removeClass('cm-dataform-errorrow' );

}

}否则{

$(当前).addClass('cm-dataform-errorrow');

ccount ++;

}

}



获取正则表达式字符串fr om ajax请求。

因为正则表达式exp是一个字符串,match()函数不起作用。

如何将字符串正则表达式转换为正确的正则表达式exp在这里验证数据输入。



我尝试过:



我尝试使用RegExp的

i have the below js code to test email

function testMatch() {
var current = $(document).find('#txtbox');
var ccount = 0;
var vcode = "/^[a-z0-9\_\-\.]+[\@]{1}[a-z0-9]+[\.]{1}[a-z]{2,}/i" /*this value actually comes from a json request*/
var value = "useremail@gmail.com";
if (value != undefined && value != null && value != '') {
var res = value.match(vcode);
if (res == null) {
$(current).addClass('cm-dataform-errorrow');
ccount++;
} else {
$(current).removeClass('cm-dataform-errorrow');
}
} else {
$(current).addClass('cm-dataform-errorrow');
ccount++;
}
}

the regex string is fetched from the ajax request.
since the regex exp is a string the match() function does not work.
How do i convert string regex exp to proper regex exp which can validate data input here.

What I have tried:

I tried using test(), exec() methods of RegExp

推荐答案

(文件).find('#txtbox');

var的test(),exec()方法ccount = 0;

var vcode =/^ [az0-9 \\\\-\.]+ [\@] {1} [a-z0-9] + [\。] {1} [az] {2,} / i/ *此值实际上来自json请求* /

var value =useremail@gmail.com;

if(value!= undefined&&值!= null&& value!=''){

var res = value.match(vcode);

if(res == null){
(document).find('#txtbox');
var ccount = 0;
var vcode = "/^[a-z0-9\_\-\.]+[\@]{1}[a-z0-9]+[\.]{1}[a-z]{2,}/i" /*this value actually comes from a json request*/
var value = "useremail@gmail.com";
if (value != undefined && value != null && value != '') {
var res = value.match(vcode);
if (res == null) {


(当前).addClass('cm-dataform-errorrow');

ccount ++;

} else {
(current).addClass('cm-dataform-errorrow');
ccount++;
} else {


(当前).removeClass('cm-dataform-errorrow');

}

} else {
(current).removeClass('cm-dataform-errorrow');
}
} else {


这篇关于如何将字符串regex exp转换为正确的regex exp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆