Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.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
Javascript 在同一行中匹配多个图案_Javascript_Regex - Fatal编程技术网

Javascript 在同一行中匹配多个图案

Javascript 在同一行中匹配多个图案,javascript,regex,Javascript,Regex,我有点像 var string = "<h1>Hi {{name|there}}</h1> <p>This is a message<br> </p> <p>Hello hello</p>" 有什么想法吗?你应该转义所有这些字符:{}| string.match(/\{\{(.+)\|(.+)\}\}/) 你可以这样做: var string=“Hi{{name | there}}}这是一条消息你好你好”;

我有点像

var string = "<h1>Hi {{name|there}}</h1>
<p>This is a message<br>
</p>
<p>Hello hello</p>"

有什么想法吗?

你应该转义所有这些字符:{}|

string.match(/\{\{(.+)\|(.+)\}\}/)

你可以这样做:

var string=“Hi{{name | there}}}这是一条消息
你好你好

”; //如果变量存在 log(解析(字符串,{name:{Tony})); //如果未设置变量 log(解析(字符串,{potato:{Tony})); 函数解析(str,值){ str=str.replace(/\{(.*)\\\\}(.*)\}}/g,函数(arg,match1,match2){ var data=match1.split(“|”); var变量=match1; var回退=匹配2; var替换=回退; if(typeof(值[变量])!=“未定义”){ 替换=值[变量]; } 退换货; }); 返回str;
}
如果您的输入看起来像
{{name}}
(=>
name
),或者
{{{那里}
(=>
那里
)或者
{there}
(=>
那里
),您可以使用以下方法:

var string=`Hi{{{name | there}
这是一个{{|消息}

你好{{你好}

`; var rx=/{{(.*?(:\\|(.*?)}}/g; console.log(string.replace(接收、功能)(m、g1、g2){ 返回g1?g1:g2;
}));您可以执行以下操作:

var str=“Hi{{name | there}”,
val1=“muçaços”,
val2=未定义,
str1=str.replace(/\{(\w+)\\\\\\}/,(m,p1,p2)=>val1 | p2),
str2=str.replace(/\{(\w+)\(\w+)\}}/,(m,p1,p2)=>val2 | p2);
控制台日志(str1);

控制台日志(str2)告诉我们你试过什么?你是说可以有
{{{name}}
(=>
name
),或者
{{{那里}
(=>
there
)或者
{{there}
(=>
there
)?
string.match(/\{\{(.+)\|(.+)\}\}/)