1

I'm trying to create the following regex using Javascript.

(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)

However, by doing this it gives me invalid group error in the console.

regExp = new RegExp("(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)", "gi");

I don't understand where the problem comes from exactly. I appreciate the help.

Thank you

EDIT: After some research I found that Javascript does not support lookbehinds.

So the error comes from (?<!\\). Refer this newly asked question to find an alternative way to do the same job. How to check for odd numbers of backslashes in a regex using Javascript?

Ziko
  • 919
  • 2
  • 10
  • 22
  • Could you provide a string you're trying to validate the RegEx against? – Fueled By Coffee May 14 '18 at 14:31
  • 4
    You need to double-escape the `\ `. You've escaped it once with `\\ `, so your regex reads: `(?<!\) ` This effectively escapes the `)`. –  May 14 '18 at 14:32
  • @FueledByCoffee any string that starts with an odd backslash following a number between 5 and 9 or double digits example 11. Let's say \9 or \11 or \\\5 – Ziko May 14 '18 at 14:32
  • @Amy Should it be like that you mean ? new RegExp("(?<!\\\)(?:\\\{2})*\\\(?!\\\)([5-9]|[1-9]\d)", "gi"); – Ziko May 14 '18 at 14:34
  • @Ziko I am marking this as not-a-duplicate. Can I suggest that you remove your answer from the question, and answer the question yourself, instead? – Anonymoose May 14 '18 at 18:57
  • @Anonymoose I can't post an answer on my post idk why.. – Ziko May 14 '18 at 19:07

1 Answers1

4

If your expression isn't dynamic, just use a literal:

var regExp = /(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)/gi;

The problem is that your escape sequences \\ inside the string end up rendering \ characters inside the regEx, which in turn end up escaping brackets they shouldn't, resulting in unterminated groups.

Máté Safranka
  • 4,081
  • 1
  • 10
  • 22
  • My expression is dynamic, taking an input binding using angular and validate it run time. However, this also gives me an error idk why, I tried it before asking the question tho – Ziko May 14 '18 at 14:38
  • I mean if the regex itself is dynamic, e.g. `var regEx = new RegExp('[a-z]' + somePattern + '$');`, then you need to use the constructor, but otherwise you can just use a literal. – Máté Safranka May 14 '18 at 14:42
  • It is, I simplified it here to be clear but actually inside [5-9], I have something like "["+num_groups+"-9]" which is a number of groups captured in another input. – Ziko May 14 '18 at 14:43
  • Ah, sorry, I misunderstood. In any case, the core of the issue is the escape sequences, check the answer linked by Wiktor. – Máté Safranka May 14 '18 at 14:45
  • I understand the escape thing in the question he provided but following it, it doesn't solve the problem and I'm still getting invalid group for this specific regex expression. – Ziko May 14 '18 at 14:47
  • Looks like the problem comes from (?<!\\). It appears that javascript do not support positive lookbehind assertions. If I remove it, everything works well. So it was not the escaping problem as mentioned above – Ziko May 14 '18 at 14:51