Javascript正则表达式不匹配 [英] Javascript Regular Expression not matching
问题描述
早上全部
我有一个javascript正则表达式无法正常工作,我不知道为什么。
I have a javascript regular expression that doesn't work correctly and I'm not sure why.
我在 https://uptimerobot.com 上调用API,并返回一个JSON字符串监视器雕像的详细信息。然而,这包含在函数调用语法中。像这样:
I'm calling the API at https://uptimerobot.com, and getting back a JSON string with details of the monitor statues. 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.
但是我似乎找不到与逻辑匹配的RegEx语法:
However I can't seem to find a RegEx syntax to match the logic:
- 字符串开头
- 未知数量的字符[a-zA-Z]
- 打开括号
- 张开大括号
- 任何字符的未知数字
- 关闭大括号
- 关闭括号
- 字符串结尾
- Start of string
- An unknown number of characters [a-zA-Z]
- Open parentheses
- Open brace
- An unknown number of any character
- Close brace
- Close parentheses
- End of string
这看起来是正确的:
^[a-zA-Z]+\(\{.*\}\)$
适用于regex101: https:// regex101。 com / r / sE7dM6 / 1
And works in regex101: https://regex101.com/r/sE7dM6/1
但是我的代码和jsFiddle失败了: https://jsfiddle.net/po49pww3/1/
However it fails in my code and via jsFiddle: https://jsfiddle.net/po49pww3/1/
m是在regex101中添加,因为实际的字符串更长,没有它就无法匹配,但是我尝试过的一些小小的调整没有在jsFiddle中产生匹配。
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.
任何人都知道什么是错的?
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");
这篇关于Javascript正则表达式不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!