Is there a way to enable regex syntax highlighting for `new RegExp` in JavaScript within Visual Studio Code (VSCode)?

Looking to achieve
syntax highlighting for "new RegExp" using regex syntax, similar to the regex literal.
Any suggestions on how to accomplish this?

https://i.sstatic.net/eIArX.png

/^this\b(.*?)\bis(:{0,1})/g; 

new RegExp(/*regex*/ `^this\b(.*?)\bis(:{0,1})`, 'g'); // << syntax higlight?

Update:

Realized that it might not be a good idea after all:
-- The escape character \ behaves differently in Strings than in Regex Literals
-- String should not be highlighted as Regex Literal (unless there's a solution for the \ escape dilemma)

Answer №1

Update:

After realizing the inherent flaws in this approach, it became evident that highlighting String as Regex Literal is problematic due to the differing meanings of the escape character \ in each context.


preface

  • While I lack expertise in this area, certain steps may be redundant or imperfect - particularly concerning overwriting the default TextMate rule.

  • If this method fails, alternative solutions should be explored.

  • It's important to note that this technique has its shortcomings, such as incorrect syntax highlighting for brackets.

shorter path

  • If setting up the vscode extension project seems daunting, consider downloading an existing project from this repository. Make necessary adjustments to the package.json &

    JavaScript.tmLanguage.json (js_new_regex_syntax_hlt.tmLanguage.json)
    and place the folder in your Vscode Extension directory.

detailed procedure

  1. Begin by creating a new folder and following the specified setup process.

  2. Research a suitable TextMate rule for JavaScript and integrate it into your project.

  3. Add a custom regex matching rule under the existing rules to account for instances of new RegExp.

    // New Regex Rule
    {
      "name": "string.regexp.js",
      "begin": "RegExp\\((`|'|\")",
      "beginCaptures": {
        "0": {
          "name": "punctuation.definition.string.begin.js"
        }
      },
      "end": "(`|'|\")",
      "endCaptures": {
        "1": {
          "name": "punctuation.definition.string.end.js"
        }
      },
      "patterns": [
        {
          "include": "#regexp"
        }
      ]
    }
    

    Make any required edits to the package.json file to ensure proper integration of the new rules.

  4. Upon completing the project setup, copy the folder to the designated Vscode Extensions directory.

  5. Reload Vscode to activate the changes and witness the new regex syntax highlighting for new RegExp.

more info

For further details and additional resources on this topic, refer to the provided Stack Overflow links and documentation sources.

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

Error encountered in React due to a syntax issue with parsing JSON from the response received

The structure of my node.js express server is as follows: const express = require('express'); const app = express(); const port = process.env.PORT || 5000; app.listen(port, () => console.log(`Listening on port ${port}`)); app.get('/exp ...

What is the best way to incorporate a Thymeleaf message stored in message.properties into my JavaScript file?

Is there a way to insert messages from a message.properties file into a javascript file similar to how it's done in an HTML file? For example, on my home page I have a title listed as: <h1 th:text="#{home.screen.title}"></h1> Where home ...

Refresh Jira Gadget AJAX while Viewing Configuration Screen

Having trouble finding a solution to this specific issue and I'm really hoping for a resolution. I am currently working on developing a Jira gadget where I have a configuration screen with two fields. The first one is a quickfind project picker that ...

encountering a problem when attempting to transfer data from JavaScript to a scriptlet in a JSP

I am currently facing a requirement where I need to transfer a value from JavaScript to a Scriplet in a JSP. Despite being aware that JavaScript runs on the client side and JSP on the server side, I have been unsuccessful in finding a suitable solution for ...

Which is more powerful: Angular's change detection or directly editing HTML?

One thing I've heard is that Angular's change detection can potentially cause lag in an application when receiving a large amount of data from a websocket. In my setup, the data comes from the socket, updates a variable in the component, and then ...

Conceal the navigation bar when scrolling to the top of the

Hey there! I'm looking for a way to hide my navigation bar when not scrolling, and then display it again once I start scrolling. Currently, I have two menus on this website: one with a white background and the other with a blue background. You can fi ...

Troubleshooting issues with jQuery and the UpdatePanel

I am developing an ASP.NET4 C# web application with jQuery in the page that uses a master page. My content placeholder is within an update panel, but the script does not work when there are postbacks. Master Page <body> <form runat ="server"> ...

Unable to retrieve all AJAX responses during page loading in Puppeteer

When loading the URL here, I notice around four AJAX calls in the network tab. However, when using Puppeteer to scrape the data, only one response URL from an AJAX call is being logged: I have attempted the following code: async function main() { try { ...

Failed Attempt to Execute React Native Application using Command Prompt (iOS)

I'm currently working through the React Native tutorial found on their official website. During my project building process, I utilized the following command: react-native run-ios An error was encountered: Found Xcode project TestProject.xcodeproj ...

To link the information within the angularJS controller

I've recently generated a div element dynamically within the controller function of my AngularJS application. However, I'm facing an issue where the data is not binding as expected inside this div element. Here is a snippet of my code: function ...

Encountering an issue in Typescript where utilizing ref in ShaderMaterial triggers an error stating: "The anticipated type is originating from the property 'ref' declared within the 'PolygonMat' type definition."

While working on a shaderMaterial with the drei library, I encountered an issue with TypeScript when using ref: Type 'RefObject<PolygonMat>' is not assignable to type 'Ref<ShaderMaterial> | undefined'. I defined the type ...

Having trouble implementing button functionality in a web app using JS, jQuery, HTML, and CSS along with an API integration

I am currently developing a web search engine that utilizes the Giphy API... However, I've encountered difficulties with the button function. I have created buttons to modify the page format and adjust the number of search results displayed.... The We ...

Improving code structure for a unique date feature

I've successfully built a date component (see GIF below). The code is functioning as intended, but I feel it's a bit convoluted and may be difficult for others to grasp. Note: Please refer to the GIF below. Ignore the styling. This is how I&ap ...

Surprising outcome caused by introducing a service dependency within another service in Angular 6

In my current project, I am facing an issue with ngrx-store and Angular 6. Unfortunately, I cannot replicate the problem on the stackblitz, so I will explain it here. I have a Service1 being used in the component, as well as a Service2 that is used within ...

An error occurred in Node.js while parsing data: Headers cannot be set after they have already been sent to the client

I am currently using a CSV parser to read data from a CSV file. I then pass this data in object format to an EJS template file for printing. However, when I try to stringify the data, I encounter an error. The error message is as follows: Error [ERR_HTTP_ ...

Methods for decoding a JSON object and iterating through its contents

After learning how to encode an object on the server side from this post, I am now interested in decoding it on the client side. Following is what I do on the client side: $.ajax({ type: "GET", url: "/cgi-bin/ajax_sort.pl", contentType: "appl ...

steps to extract routing into its own component

Is it possible to extract this routing logic into a separate component? I attempted to use map but it didn't work I want to have only a single route, and change the 'path' when a specific link is clicked const Home = ({ code }) => { re ...

Utilizing React to customize JVectorMap markers

Having an issue with a marker appearing in my React project https://i.stack.imgur.com/hkqnZ.jpg The markers are displaying correctly, but there is a persistent initial marker at 0,0 that I can't remove. Removing the 'markers' property from ...

Tips for writing an async function using TypeScript

I've been working with Typescript and NLP.js. However, I'm encountering an issue where the argument manager is displaying 'Parameter manager implicitly has an any type'. I attempted to use :, but it didn't solve the problem eff ...

Is there a way to compare data before and after revalidation using useSWR?

With the use of Next.js and through the implementation of useSWR, data is fetched from an endpoint every 30 seconds using an automatic revalidation interval (https://swr.vercel.app/docs/revalidation#revalidate-on-interval). Within the retrieved data, there ...