问题
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