I am using Nodejs to build an application in which I need to process certain strings I have used the JS "RegExp" object for this purpose. I want only a part of my string in the regex to be case insensitive
var key = '(?i)c(?-i)ustomParam';
var find = '\{(\\b' + key +'\\b:?.*?)\}';
var regex = new RegExp(find,"g");
But it breaks with following error
SyntaxError: Invalid regular expression: /{(\b(?i)c(?-i)ustomParam\b:?.*?)}/
I will get the key from some external source like redis and the string to be matched from some other external source , I want that the first alphabet should be case-Insensitive and the rest of alphabets to be case-Sensitive.
When I get the key from external source I will append the (?i) before the first alphabet and (?-i) after the first alphabet.
I even tried this just for starters sake, but that also didn't work
var key ='customParam';
var find = '(?i)\{(\\b' + key +'\\b:?.*?)\}(?-i)';
var regex = new RegExp(find,"g");
I know I can use "i" flags instead of above ,but that's not my use case. I did it just to check.
I am using Nodejs to build an application in which I need to process certain strings I have used the JS "RegExp" object for this purpose. I want only a part of my string in the regex to be case insensitive
var key = '(?i)c(?-i)ustomParam';
var find = '\{(\\b' + key +'\\b:?.*?)\}';
var regex = new RegExp(find,"g");
But it breaks with following error
SyntaxError: Invalid regular expression: /{(\b(?i)c(?-i)ustomParam\b:?.*?)}/
I will get the key from some external source like redis and the string to be matched from some other external source , I want that the first alphabet should be case-Insensitive and the rest of alphabets to be case-Sensitive.
When I get the key from external source I will append the (?i) before the first alphabet and (?-i) after the first alphabet.
I even tried this just for starters sake, but that also didn't work
var key ='customParam';
var find = '(?i)\{(\\b' + key +'\\b:?.*?)\}(?-i)';
var regex = new RegExp(find,"g");
I know I can use "i" flags instead of above ,but that's not my use case. I did it just to check.
Share Improve this question edited Jan 5, 2024 at 8:16 Mahozad 24.6k19 gold badges159 silver badges179 bronze badges asked May 13, 2016 at 10:43 VipreshVipresh 1,30717 silver badges34 bronze badges 3-
1
You cannot make a part of regex case-insensitve, use
i
flag on plete regex. – Tushar Commented May 13, 2016 at 10:45 -
3
Bad news: even XRegExp cannot offer this functionality, you can only use
(?i)
on the whole pattern declaring it at the pattern beginning. In XRegExp, you can define the regex asvar regex = XRegExp('(?i)\\{(\\b' + key +'\\b:?.*?)\\}', 'g');
– Wiktor Stribiżew Commented May 13, 2016 at 10:50 - 1 OKi so JavaScript regex library sucks. @WiktorStribiżew if you had posted your ment as answer I would have accepted it as answer – Vipresh Commented May 13, 2016 at 11:03
2 Answers
Reset to default 12JavaScript built-in RegExp
does not support inline modifiers, like (?im)
, let alone inline modifier groups that can be placed anywhere inside the pattern (like (?i:....)
).
Even XRegExp cannot offer this functionality, you can only use (?i)
on the whole pattern declaring it at the pattern beginning.
In XRegExp, you can define the regex ONLY as
var regex = XRegExp('(?i)\\{(\\b' + key +'\\b:?.*?)\\}', 'g');
On May 27, 2020, still neither JavaScript native RegExp, nor XRegExp patterns support inline modifier groups (i.e. (?i:...)
), nor placing them in any part of the pattern (as far as XRegExp is concerned).
This will be possible starting from Chrome 122 using RegExp modifiers.
To enable a flag for a subexpression, use (?X:subexpr)
where X is one of i
, m
, or s
.
To disable a flag for a subexpression, use (?-X:subexpr)
.
Enable a flag for part of regex:
const r1 = /^[a-z](?i:[a-z])$/;
r1.test("ab"); // true
r1.test("Ab"); // false
r1.test("aB"); // true
Disable a flag for part of regex:
const r2 = /^[a-z](?-i:[a-z])$/i;
r2.test("ab"); // true
r2.test("Ab"); // true
r2.test("aB"); // false