Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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,我需要一个正则表达式来匹配: ["4","0","2"] ["4564","320","452"] ["23"] 我测试了一个正则表达式,它在那里工作得很好。 因此,我用JavaScript编写了它: let ar_value = '["4564","320","452"]'; let pattern = '^\[(,?"[0-9]+")+\]$'; let matches = ar_value.match(pattern); console.log(matches); 但这导致了错误:

我需要一个正则表达式来匹配:

["4","0","2"]
["4564","320","452"]
["23"]
我测试了一个正则表达式,它在那里工作得很好。

因此,我用JavaScript编写了它:

let ar_value = '["4564","320","452"]';
let pattern = '^\[(,?"[0-9]+")+\]$';
let matches = ar_value.match(pattern);
console.log(matches);
但这导致了错误:

Uncaught SyntaxError: Invalid regular expression: /^[(,?"[0-9]+")+]$/: Unmatched ')'
有人能看出我做错了什么

非常感谢

让ar_值='[“4564”、“320”、“452”];
让模式=“^\[(,?“[0-9]+”+\]$”;
让matches=ar_值。match(模式);

console.log(匹配项)
JSON.parse('[“4564”、“320”、“452”]')
?顺便说一句,
)+
是不匹配的。您需要在构造函数表示法中加倍转义反斜杠。或者最好使用正则表达式文本,
let pattern=/^\[(,?“[0-9]+”)+]$/改为使用正则表达式文字,而不是字符串,否则您将不得不对反斜杠进行双重转义。@WiktorStribiżew,它工作正常!非常感谢你!但是,我在模式上做错了什么?您忘记在字符串中使用文字反斜杠,而在字符串文字中使用了未定义的转义序列。@WiktorStribiżew再次感谢!:)