简体   繁体   English

Javascript 正则表达式不匹配

[英]Javascript Regular Expression not matching

Morning All大家早

I have a javascript regular expression that doesn't work correctly and I'm not sure why.我有一个无法正常工作的 javascript 正则表达式,我不知道为什么。

I'm calling the API at https://uptimerobot.com , and getting back a JSON string with details of the monitor statues.我正在https://uptimerobot.com 上调用 API,并返回一个包含监视器状态详细信息的 JSON 字符串。 This is however wrapped in a function call syntax.然而,这包含在函数调用语法中。 Like this:像这样:

jsonUptimeRobotApi({MASKED-STATUES-OBJ})

As this call is being made from a generic script I was hoping to test the response to see if it had this type of syntax wrapping then parse it accordingly.由于此调用是从通用脚本进行的,我希望测试响应以查看它是否具有这种类型的语法包装,然后相应地解析它。

However I can't seem to find a RegEx syntax to match the logic:但是我似乎无法找到匹配逻辑的 RegEx 语法:

  • Start of string字符串的开始
  • An unknown number of characters [a-zA-Z]未知数量的字符 [a-zA-Z]
  • Open parentheses开括号
  • Open brace开大括号
  • An unknown number of any character任何字符的未知数量
  • Close brace闭合大括号
  • Close parentheses右括号
  • End of string字符串结束

This looks right:这看起来是对的:

^[a-zA-Z]+\(\{.*\}\)$

And works in regex101: https://regex101.com/r/sE7dM6/1并在 regex101 中工作: https ://regex101.com/r/sE7dM6/1

However it fails in my code and via jsFiddle: https://jsfiddle.net/po49pww3/1/但是它在我的代码中和通过 jsFiddle 失败: https ://jsfiddle.net/po49pww3/1/

The "m" was added in regex101 as the actual string is much longer, and failed to match without it, however a number of small tweeks that I've tried havn't resulted in a match in jsFiddle. “m”是在 regex101 中添加的,因为实际的字符串要长得多,没有它就无法匹配,但是我尝试过的一些小 tweeks 没有在 jsFiddle 中产生匹配。

Anyone know whats wrong?有谁知道怎么了?

Escape all the backslashes one more time because within " delimiters, you must escape the backslash one more time or otherwise it would be treated as an escape sequence.再次转义所有反斜杠,因为在"分隔符中,您必须再次转义反斜杠,否则它将被视为转义序列。

var regEx = new RegExp("^[a-zA-Z]+\\(\\{.*\\}\\)$", "m");

DEMO演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM