3

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.

5
  • What effect does it have in Safari and Chrome? Commented Dec 11, 2010 at 4:42
  • 3
    Webkit will happily ingest /string/zxcvbgi, ignoring the invalid zxcvb, and still applying the valid gi flags. Not so Firefox. Commented Dec 11, 2010 at 4:51
  • @Ken Redler - please post an answer and I will accept it. Commented Dec 11, 2010 at 7: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...) " 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. Commented Aug 10, 2021 at 21:40
  • Judging from your code, you don't need the indices, because you aren't using subgroups. Commented Aug 10, 2021 at 21:42

4 Answers 4

7

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 ]
]
Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't seem to work in TypeScript though... (at least not for me)
Thank you for saying flat-out, "It does not change the regex's interpretation or matching behavior in any way". The W3 documentation doesn't not make that clear, and the "Try it" example provided doesn't show the indices (which are the whole point) in any way.
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.

Comments

1

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

  • g - Global search (multiple matches)
  • i - Case insensitive
  • m - Multiple input

3 Comments

developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… --- "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.
This answer 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.
According to MDN, it was implemented in Firefox v88 and Chrome v90.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.