最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What does the d flag mean in a javascript RegEx? - Stack Overflow

programmeradmin3浏览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: 
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.

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/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.

Share Improve this question asked Dec 11, 2010 at 4:39 vkraemervkraemer 9,9122 gold badges31 silver badges44 bronze badges 5
  • What effect does it have in Safari and Chrome? – BoltClock 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. – Ken Redler Commented Dec 11, 2010 at 4:51
  • @Ken Redler - please post an answer and I will accept it. – vkraemer 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. – Craig Hicks Commented Aug 10, 2021 at 21:40
  • Judging from your code, you don't need the indices, because you aren't using subgroups. – Craig Hicks Commented Aug 10, 2021 at 21:42
Add a ment  | 

4 Answers 4

Reset to default 6

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 ]
]

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.

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

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

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 remend you follow a good tutorial, rather than just cutting and pasting.

发布评论

评论列表(0)

  1. 暂无评论