1

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 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

3 Comments

Because using a string literal :)
Thanks guys, had just got there myself jsfiddle.net/po49pww3/4 Always the way, typing the question gives you enough of a break to look at the problem afresh.
☆🍷 CoNgRaTs 🎇 100k 🍸 Avinash! 🎉

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.