Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Part of String Case Insensitive in JavaScript Regex (?i) option not working

I am using Nodejs to build 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.

like image 963
Vipresh Avatar asked May 13 '16 10:05

Vipresh


People also ask

How do you make a section insensitive in a regex case?

Perl lets you make part of your regular expression case-insensitive by using the (? i:) pattern modifier. Modern regex flavors allow you to apply modifiers to only part of the regular expression.

How do you make a case-sensitive in regex?

In Java, by default, the regular expression (regex) matching is case sensitive. To enable the regex case insensitive matching, add (?) prefix or enable the case insensitive flag directly in the Pattern.

Is JavaScript regex case-sensitive?

Regular expression, or simply RegEx JavaScript allows you to write specific search patterns. You can also make the search case-sensitive or insensitive, search for a single JavaScript RegEx match or multiple, look for characters at the beginning or the end of a word.

How do you check if a string contains a substring case insensitive JavaScript?

To do case insensitive search, you can use regular expressions and the String#match() function, or you can convert both the string and substring to lower case using the String#toLowerCase() function.


1 Answers

JavaScript 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).

like image 192
Wiktor Stribiżew Avatar answered Oct 18 '22 21:10

Wiktor Stribiżew