Search code examples
javascriptregexvisual-studio-2015lookbehind

Visual Studio 2015 CE Javascript editor not recognizing '(?' in regex


I'm looking for your help because I'm having a weird issue with the Visual Studio 2015 editor. I have a javascript file that contains a regular expression that uses a lookbehind, something like this:

var regexStr = /(?<=[0-9a-zA-Z]+)(\/{2,})(?=[0-9a-zA-Z]+)/;

For some reason, the question mark is "breaking" the syntax and the code editor shows it as an error, although the regex is working as expected.

Do you guys know if the '(?' is defined as a special char in the Visual Studio 2015 editor?

Greetings fellow programmers!


Solution

  • First thing I would say is that Visual Studio 2015 is a sub-optimal tool for JavaScript development. There are much better tools for web development that are available for free. This could potentially be seen as opinion based, but developers vote with their feet when it comes to tooling and what makes their workflow easier and faster, and very few people are picking VS 2015 for JS development in 2020. So I have to ask, is there a reason you are locked into using particular piece of software and version for JavaScript? Are you open to using a different tool? If so, comment below and I will add a list of choices for editors and IDE's that are more popular choices and that will most likely make your life as a developer easier.

    Assuming though that you want to continue using VS 2015, regardless of its myriad flaws, and just want this particular thing solved, I think that what you are seeing may be the result of updated ECMAScript/JavaScript syntax leaving VS 2015 in the dust. It could also be that this was always just a bug. I first suggest ensuring that you have updated the base application as much as VS 2015 will allow you to. Are you using a pro or community version? Even if you stuck with Visual Studio, you could still get a more recent year's version, and if you are already using the community version that update would be free. Once you are sure you have updated the software as much as possible, I would suggest looking for an extension that could help you with this particular issue.

    You can search the Visual Studio Extension Marketplace yourself, and I would look for one that offers things like "JavaScript syntax highlighting" or "updated JavaScript grammar".

    Here is an extension I just found that may help you as well:

    • Regex Editor - "IntelliSense, syntax coloring, in-place testing and more for your regular expressions, right inside the editor!"