Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Javascript删除正则表达式分隔符_Php_Javascript_Regex_Json - Fatal编程技术网

Php Javascript删除正则表达式分隔符

Php Javascript删除正则表达式分隔符,php,javascript,regex,json,Php,Javascript,Regex,Json,我的PHP代码根据用户输入生成JSON 在这个JSON中,我有一个带有分隔符的正则表达式字符串“/Regex/gi” 我将其发送给用户,并希望javascript基于该正则表达式测试字符串。但是因为我在字符串中有分隔符,所以它不起作用 有时,我可以接收regex字符串作为“regex”、“/regex/”或“/regex/gi” 那么有没有办法删除这些分隔符,或者转换正则表达式中的字符串呢 我试图使用“newregexp”,但它转义了字符串,并且无法工作 下面是一些代码: var json =

我的PHP代码根据用户输入生成JSON

在这个JSON中,我有一个带有分隔符的正则表达式字符串“/Regex/gi”

我将其发送给用户,并希望javascript基于该正则表达式测试字符串。但是因为我在字符串中有分隔符,所以它不起作用

有时,我可以接收regex字符串作为“regex”、“/regex/”或“/regex/gi” 那么有没有办法删除这些分隔符,或者转换正则表达式中的字符串呢

我试图使用“newregexp”,但它转义了字符串,并且无法工作

下面是一些代码:

var json = {regex: "/hello/"}; //This code is generated by PHP
"hello".match(json.regex); //Test in javascript, not working, since the "/" are inside the regex and not used as delimiter 
有人知道我该怎么做吗

谢谢

更多推进方式:

var json = {regex: "hello"};
var reg = new RegExp(json.regex)
var matched = reg.exec("hello") != null;
更多推进方式:

var json = {regex: "hello"};
var reg = new RegExp(json.regex)
var matched = reg.exec("hello") != null;

你可以把你的正则表达式正则化(woah)


你可以把你的正则表达式正则化(woah)


代码样本将是有用的。一些代码请,我们需要see@balajimcavar json={regex:“/hello/”}//由php生成,“hello.match”(json.regex)//不工作,仅当字符串为“/hello/”bro时工作,您可以使用代码更新您的问题。不在注释部分:)您应该接受一种格式,否则会遇到问题。我将向您展示一个示例:user1尝试
/\/\/
,user2尝试
/\/\//\//gi
和user3
\/\/
(不带分隔符)。如果我们从开头和结尾删除
/
,第三个用户将有问题。代码示例将很有用。请输入一些代码,我们需要see@balajimcavar json={regex:“/hello/”}//由php生成,“hello.match”(json.regex)//不工作,仅当字符串为“/hello/”bro时工作,您可以使用代码更新您的问题。不在注释部分:)您应该接受一种格式,否则会遇到问题。我将向您展示一个示例:user1尝试
/\/\/
,user2尝试
/\/\//\//gi
和user3
\/\/
(不带分隔符)。如果我们从开头和结尾删除了
/
,第三个用户将有问题。我非常希望不要使用eval。我尝试了新的RegExp类,但是如果输入是“/hello/”RegExp将转义“/”,我需要一种方法来简单地删除它们。我非常不希望使用eval。我尝试了新的RegExp类,但是如果输入是“/hello/”RegExp将转义“/”,我需要一种方法来简单地删除它们。
var reMeta = /(^|[^\\])\/(\w+$){0,1}/;
var json = {regex: "/hello\/slash\//gi"};

json.regex = new RegExp( json.regex.replace(reMeta,'$1') );
// after replace it looks like: "hello\/slash\/"