Javascript Regular Expression not matching

烈酒焚心 提交于 2019-12-29 01:57:50

问题


Morning All

I have a javascript regular expression that doesn't work correctly and I'm not sure why.

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.

However I can't seem to find a RegEx syntax to match the logic:

  • 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

This looks right:

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

And works in regex101: https://regex101.com/r/sE7dM6/1

However it fails in my code and via 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.

Anyone know whats wrong?


回答1:


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



来源:https://stackoverflow.com/questions/32115028/javascript-regular-expression-not-matching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!