BOUTERBIAT Oualid BOUTERBIAT Oualid -4 years ago 134
Javascript Question

JavaScript regular expression ignore case exception (Invalid group)

I have the following Regular expression :

(?i:(?:(?:(?:fbx|fo))\d+)|(?:(09|0[1-5])\s?(?:\d{2}\s?){4})(?:(@freeadsl)?))


I tested the expression in https://regex101.com/ and it works.

enter image description here

But in javascript, it dosen't work.
enter image description here

After doing a search, it turned out that the problem is that javascript doesn't accept regex ignore case
?i
.

What's the best solution to remedy this problem.
Any help, i'll appreciate it, thanks !

Answer Source

JavaScript regex engine does not support inline modifier groups. You may use a i modifier in the JS regex and remove unnecessary non-capturing groups to reduce your regex to

var rx = /(?:fbx|fo)\d+|(?:09|0[1-5])\s?(?:\d{2}\s?){4}(?:@freeadsl)?/i;
                                                                      ^

See the regex demo. The /i at the end makes the letters in the pattern match both lower- and uppercase letters.

Details:

  • (?:fbx|fo)\d+ - fbx or fo substring followed with 1+ digits
  • | - or
  • (?:09|0[1-5]) - 09 substring or 0 followed with 1 to 5 digit.
  • \s? - an optional (1 or 0) whitespaces
  • (?:\d{2}\s?){4} - 4 occurrences of:
    • \d{2} - 2 digits
    • \s? - an optional (1 or 0) whitespaces
  • (?:@freeadsl)? - an optional @freeadsl substring.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download