0

I have a regex, that appears to work in Safari and Chrome that gives the following error in FireFox.

Error: invalid regular expression flag d
Source File: http://java.net/projects/mq
Line: 194, Column: 34
Source Code:
    var vbkpatt1=/projects\/[^/]+/di; 

I had fought with this RegEx a couple weeks ago and had put it aside, so I do not have a link to the page that led me to use the 'd' flag. A pointer to a ref that includes the d flag would be a solid start to resolving my issue.

vkraemer
  • 9,864
  • 2
  • 30
  • 44
  • What effect does it have in Safari and Chrome? – BoltClock Dec 11 '10 at 04:42
  • 3
    Webkit will happily ingest `/string/zxcvbgi`, ignoring the invalid `zxcvb`, and still applying the valid `gi` flags. Not so Firefox. – Ken Redler Dec 11 '10 at 04:51
  • @Ken Redler - please post an answer and I will accept it. – vkraemer Dec 11 '10 at 07:39
  • ["If the match succeeds, the exec() method returns an array (with extra properties index, input, and if the d flag is set, indices; see below...) "](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#syntax) is a ref. However, if you rely on 'd' to get indices and need them, your code won't work without out, making the selected answer unsuitable. It's 2021 and the 'd' flag is documented by not actually present on most platforms. – Craig Hicks Aug 10 '21 at 21:40
  • Judging from your code, you don't need the indices, because you aren't using subgroups. – Craig Hicks Aug 10 '21 at 21:42

4 Answers4

4

There is no d flag, which is your issue :) There are:

  • g - Global search (multiple matches)
  • i - Case insensitive
  • m - Multiple input
Community
  • 1
  • 1
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#syntax --- "If the match succeeds, the exec() method returns an array (with extra properties index, input, and if the d flag is set, indices; see below) ...". At the bottom of the page it shows all browser (even IE) and nodejs supporting. HOWEVER - using the 'd' flag in nodejs throws an error. Without the 'd' flag you can get the text (and the length, which is redundant info) of an arbitrary group in the match, but not the starting index of the group. – Craig Hicks Aug 10 '21 at 21:15
  • [This answer](https://stackoverflow.com/a/57642010/4376643) has information about a proposal "currently at stage 3", meaning it will eventual be part of JS, that will provide the indices as well. My guess is that that the 'd' flag is that, included in the documentation prematurely, but maybe my guess is wrong. – Craig Hicks Aug 10 '21 at 21:31
  • According to [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices), it was implemented in Firefox v88 and Chrome v90. – Mr Lister Aug 05 '22 at 06:00
2

Webkit browsers are more tolerant in this case, and will accept something like this:

/theregex/zxcvbgi

Instead of throwing am error, they see it as:

/theregex/gi

Firefox, however, will object to any invalid flags. Nick points out the valid ones in his answer.

Ken Redler
  • 23,863
  • 8
  • 57
  • 69
1

Since ECMAScript 2022, there IS a "d" flag.

From MDN Web Docs: The d flag indicates that the result of a regular expression match should contain the start and end indices of the substrings of each capture group. It does not change the regex's interpretation or matching behavior in any way, but only provides additional information in the matching result.

const regexWithOutDFlag = /hi(\d)/g;
const hi = 'hi1hi2';
const result = [...hi.matchAll(regexWithOutDFlag)];
console.log(result[0]);

// output:
[ 'hi1', '1', index: 0, input: 'hi1hi2', groups: undefined ]

const regexWithDflag = /hi(\d)/gd;
const hi2 = 'hi1hi2';
const result2 = [...hi2.matchAll(regexWithDflag)];
console.log(result2[0]);

// output:
[
  'hi1',
  '1',
  index: 0,
  input: 'hi1hi2',
  groups: undefined,
  indices: [ [ 0, 3 ], [ 2, 3 ], groups: undefined ]
]
Andrejs
  • 10,803
  • 4
  • 43
  • 48
0

Are you sure it actually does something in Chrome? I tried:

/projects\/[^/]+/zyx

It accepts that as /projects\/[^/]+/, but I strongly doubt those are all real extensions. It's just ignoring them. as noted by Ken, it will keep valid flags even if invalid ones are present.

Also, I recommend you follow a good tutorial, rather than just cutting and pasting.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539