Reputation: 9892
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.
Upvotes: 1
Views: 4166
Reputation: 11921
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 ]
]
Upvotes: 4
Reputation: 630429
There is no d
flag, which is your issue :) There are:
g
- Global search (multiple matches)i
- Case insensitivem
- Multiple inputUpvotes: 2
Reputation: 23943
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.
Upvotes: 2
Reputation: 284826
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.
Upvotes: 0