Fresh Regular Expression created from a readable string

I can't figure out why rg = new RegExp(`/${a}.*${b}/`) isn't functioning properly here, while rg = /\(.*\)/ is working fine.

let a = '\(';
let b = '\)';
let rg;
//rg = new RegExp(`/${a}.*${b}/`); // doesn't work
rg = /\(.*\)/;
let match = rg.exec(`test (regex works) is ok`);
if (match) {
  console.log(match[0]); // -> (regex works)
}

Answer №1

The reason is that in order to properly include the backslash character `\` in the string, you must double escape it.

const x = '\\\\(';
const y = '\\\\)';

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

Adding Jquery to Codeigniter version 2.X

I've been trying to incorporate Jquery into my codeigniter 2.X project without success. I added the library link and confirmed in the debugger that it loads without any errors. However, when I try to use the Document ready function, it simply doesn&ap ...

Discover the subsite inventory of a SharePoint site using TypeScript

Is there a way to gather all sub-sites from my SharePoint site and organize them into a list? I initially thought of using this.context.pageContext, but could not locate it. Please excuse my seemingly simple question, as I am still learning TypeScript. ...

If Gulp is running continuously, what output will I receive?

After reading a helpful post on StackOverflow about gulp tasks (viewable here), I came to the conclusion that it's not advisable to omit returning anything in a gulp task. For proper synchronization of asynchronous tasks, the caller should wait. Pres ...

The useEffect hook is failing to trigger within the Higher Order Component (Hoc

My component was working fine without using a HOC, but now that I've wrapped it inside withSpinner HOC, the fetching process does not start. const CollectionPage = (props) => { const { isCollectionLoaded, isCollectionFetching } = props; useEf ...

What is the reason for the index type being defined twice?

Here is an example from the official TypeScript documentation: class Animal { name: string; } class Dog extends Animal { breed: string; } // Error: indexing with a 'string' will sometimes get you a Dog! interface NotOkay { [x: numbe ...

Organize a collection of strings by sorting them according to a specific character within each string

I am attempting to arrange an array of strings based on a character within each string. Here is what I have so far: function sortStrings(s) { let arr = s.split(' '); let letterArr = []; let sortedArr = []; let n = 0; for (var i = 0; ...

Choose an option to adjust the transparency of the image

I'm struggling with a select option and need some assistance. I have a select dropdown list where I want the image opacity to change from 0.3 to 1 when selected or onchange event occurs. The catch is, I can't use the value of the option because i ...

Trouble with locating newly created folder in package.json script on Windows 10

I am facing an issue in my Angular application where I am trying to generate a dist folder with scripts inside it, while keeping index.html in the root folder. I have tried using some flag options to achieve this but seem to be stuck. I attempted to automa ...

How can I trigger an audio element to play using onKeyPress and onClick in ReactJS?

While attempting to construct the Drum Machine project for freeCodeCamp, I encountered a perplexing issue involving the audio element. Despite my code being error-free, the audio fails to play when I click on the div with the class "drum-pad." Even though ...

An unexpected error occurred in the Ember route processing: Assertion Failed in the listing

After working diligently to integrate an Emberjs front end (utilizing Ember Data) with my Flask API backend, I have encountered a puzzling issue. Despite the adapter functioning correctly - as evidenced by the API being called when accessing the 'List ...

What is the method for cancelling an HTTP request in the prototype JavaScript library?

Below is the code snippet: var pendingRequest = new Ajax.Request(myUrl, { method: 'post', postBody: soapMsg, contentType: "text/xml", onSuccess: function(transport) { ...

Transferring information to a partial view using a jQuery click event

In my Index view, there is a list of links each with an ID. My goal is to have a jQueryUI dialog box open and display the ID when one of these links is clicked. Currently, I am attempting to use a partial view for the content of the dialog box in order to ...

The 'jsx' property in tsconfig.json being overridden by Next.js and TypeScript

Is there a way to prevent NextJS from changing the 'jsx' property in my tsconfig.json file from 'react' to 'preserve' when running the development server? This is how my tsconfig.json file looks: "compilerOptions": { " ...

Using Jquery Mobile to make an AJAX POST request with XML

Is it possible to use this code for XML parsing? I have successfully parsed using JSON, but there is no response from the web service. This is the status of the webservice: http/1.1 405 method not allowed 113ms $j.ajax({ type: "GET", async: false, ...

Leveraging Shared Workers within a React application

I am currently working on a backend app that continuously sends events to my React app through Web Sockets. One of the requirements is for a new browser tab to be opened when a specific event is received. However, since the application will be used by a si ...

Looking to showcase a .tif image in your Angular project?

This code is functioning properly for .png images. getNextImage(imageObj:{imageName:string,cityImageId:number,imgNumber:number}):void{ this.imgNumber= imageObj.imgNumber; this.imagePath=`assets/images/${imageObj.imageName}.png`; this.cityIma ...

Stop the controller from reloading when navigating in Angular2/Ionic2

Initially, I developed my app using tabs. When navigating to a page, the view would load for the first time (fetch data from API and display it), and upon returning to the same page, nothing would reload because the controller did not run again. Recently, ...

Is Your CanvasJS Chart Traveling in Reverse?

My charts are displaying dates in reverse order, can anyone help me figure out what's causing this issue? I've checked the documentation but couldn't find anything that would explain this problem. Link to documentation: Here is a screensh ...

Testing AG Grid's TypeScript in-line cell editing using Jest

I am currently working on writing Jest tests to evaluate the functionality of my ag-grid table. So far, I have created tests to check the default data in the grid and to test a button that adds an additional row of data to the grid. I am now attempting t ...

What is the best way to send URL encoded JSON data and receive a response?

I have been attempting to send URL-encoded data using the code snippet below: $.post( "url",{param:"value"},function(data){ alert("data==="+data); }); In this case, the URL is a restful API URL. Unfortunately, this approach was not successful. I the ...