Issues with JavaScript Regular Expressions failing to match patterns

Good morning!

I've encountered an issue with a JavaScript regular expression that I can't seem to troubleshoot.

My script is making a call to the API provided by , and receiving a JSON response containing monitor statuses. However, this JSON string is wrapped in a function call syntax like this:

jsonUptimeRobotApi({MASKED-STATUES-OBJ})

Since this call is part of a generic script, I wanted to test the response to check for this specific syntax wrapping so I could parse it properly.

But unfortunately, my attempts to create a regex pattern that matches the required logic have been unsuccessful:

  • 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

My regex pattern currently looks like this:

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

It seems to work in regex101: https://regex101.com/r/sE7dM6/1

However, when implemented in my code and tested on jsFiddle: https://jsfiddle.net/po49pww3/1/

I had to add the "m" in regex101 due to the actual length of the string, but even after tweaking, I couldn't get it to match in jsFiddle.

Does anyone have insights into what might be causing this issue?

Answer №1

Make sure to double escape all backslashes when using them within " quotation marks, as failing to do so can result in unexpected behavior.

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

Click here for a DEMO

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Utilizing Node.js to slice and manipulate bits in a

I have a chip transmitting data via Bluetooth LE to a node.js server. Here's the firmware code snippet: uint16_t txBuf[5] top &= 0x3FF; bottom &= 0x3FF; txBuf[0] = top + (bottom << 10); txBuf[1] = bottom >> 2; The first 10 bits ...

Optimal method for simultaneously updating multiple states and triggering a function after all state updates are completed [React Functional component]

I find myself in a situation where I have several controlled input components that are storing data in states. Whenever I modify one of the dropdown inputs, it also affects three other states and initiates an API call. How can I make sure that all three ...

Is there a way to instruct npm to refrain from installing any dependencies when users install my npm module?

After developing and publishing an NPM module, I noticed that it does not require any additional dependencies to function properly since it is already fully built. Despite this, when the module is installed in a different repository, it somehow triggers u ...

Use jQuery's change method to initiate a hidden file input

Want to create a fake file input using an anchor tag and trigger the hidden file input with jQuery? Looking for some advice on how to make this happen. Check out my current attempt here. I'm not sure if I'm on the right track with this, so any g ...

What could be causing Ajax's html() function to create a loop that never ends?

Is there a way to incorporate an animation during the database operation and update the table content in the HTML document once the operation is completed? <script> var jobj = new Object(); jobj.uid = <?php echo "'".$_SESSION['user_ ...

Pattern matching to eliminate line breaks and tabs

Hey there, I'm working with a string: "BALCONI \n\n\t\t\t\t10-pack MixMax chocolade cakejes" and trying to tidy it up by removing unnecessary tabs and new lines. I attempted using .replace(/(\n\t)/g, '&apo ...

Combining arrays of objects in JavaScript

I am currently working on combining different arrays: const info1 = {id: 1} const info2 = {id: 2} const info3 = {id: 3} const array1 = [info1, info2] const array2 = [info1, info3] const array3 = [info2, info3] const union = [...new Set([...array1, ...arr ...

Ways to remove a task in ReactJs agendas?

I am currently working on a code to delete an item from a list. However, I have encountered a problem where it is deleting items in a FIFO order instead of according to the index. export default class Todo extends Component{ constructor(){ supe ...

Leveraging the combination of <Form>, jQuery, Sequelize, and SQL for authentication and navigation tasks

My objective is to extract the values from the IDs #username-l and #pwd-l in an HTML form upon the user clicking the submit button. I aim to compare these values with those stored in a SQL database, and if they match exactly, redirect the user to a specifi ...

Stay updated with the complex query change feed in RethinkDB

When faced with a query like the following: r.db('universe') .table('Star') .getAll( r.db('universe').table('Ship').get(idShip)('idCurrentGalaxy'), {index: 'idGalaxy'} ) .changes ...

What is the best way to change a javascript string into HTML so that it can be shown in a form?

I am struggling to find a solution to this issue. I am fairly new to jQuery and JavaScript, so please forgive me if my question seems basic. I am trying to call a cfc (ColdFusion) using jQuery and retrieve HTML data. However, when I receive the data, it ...

Enhance your website with a dynamic div zoom feature using the Jquery Zoom

I've been scouring the internet for a jQuery plugin that allows me to zoom in on an image. There are many options out there, but I prefer ones that display the zoomed-in image in a separate area rather than directly on the original image. For example ...

Using JavaScript to Render an Array in HTML through Looping (with JSON)

I am facing an issue where only the first value of my associative array is being returned when I loop through it in my HTML file. However, when I check it using console.log, all data is displayed correctly. There is no error displayed in the HTML, but not ...

Utilizing Cordova and jQuery-mobile to launch links in a separate device browser

My app has numerous links, all of which I have added rel='external' target='_blank' to. While this works perfectly in the Ripple emulator and regular desktop browsers, it doesn't function correctly on my Android device running JB 4 ...

The audio must start playing prior to being forwarded to a new page

As I delve into the world of website development on my own, I have encountered an interesting challenge. At the top of my webpage, I have embedded an audio file within a button. If the user chooses to mute the audio, the navigation links will remain silent ...

JavaScript: Locate a property and its corresponding value within a JSON object

Dealing with a JSON object that varies in structure, but always contains a key. Any suggestions on how to extract this information? For example: "Records": { "key": "112" } Or "Records": { "test": { "key": "512" } } Or even within ...

Error: Failed to set the 'src' property of null when attempting to change the image source

When I click on an image, I want to change its source. Here is the HTML code: <div class="character"> <input id="1200" name="vocation_select" type="radio" value="1200" style="display: none" ></input> <label id="label_profesji" for="12 ...

Guide to effortlessly loading files with designated decorators in a node.js application

Within the realm of Project A, I have constructed a decorator and am seeking a method to automatically load all these decorators upon initializing the application in Project B, which is the project utilizing Project A. Is there a way to accomplish this tas ...

Warning: React Element creation caution with flow-router integration

Whenever I incorporate the following code into my Meteor app: Home = FlowRouter.route("/", { name: "App", action(params) { ReactLayout.render(App); } }); An error message pops up in the Chrome console: Warning: React.createElement: type shoul ...

Magento email confirmation message following form submission leads to a blank page

I followed a helpful tutorial on creating a custom contact form module in Magento: However, I seem to be having trouble with displaying the success message as described. Even when trying to call it like this: Mage::getSingleton('core/session')- ...