Case Insensitive Regex in JavaScript

Case insensitive regex in JavaScript

You can add 'i' modifier that means "ignore case"

var results = new RegExp('[\\?&]' + name + '=([^&#]*)', 'i').exec(window.location.href);

variable case insensitive regex match javascript

Usually, in regex, there are those useful things:

  • (?i) : starts case-insensitive mode
  • (?-i) : turns off case-insensitive mode

So, you would be able to write (?i)One(?-i)TWO. That will make One case-insensitive and TWO case-sensitive.

However, they are not supported in JavaScript.

Now, since you said that you will know your regex search string at runtime; then, you have two options:

  1. Create 2 regex search strings, one with \i and one without \i

var str = "one two three four five";
var insens_re = RegExp("One", "ig");var sens_re = RegExp("TWO", "g");
console.log(insens_re);console.log(sens_re);
var a = str.match(insens_re) || [];var b = str.match(sens_re) || [];
console.log("Matches : ", a.concat(b));

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

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

javascript includes() case insensitive

You can create a RegExp from filterstrings first

var filterstrings = ['firststring','secondstring','thridstring'];
var regex = new RegExp( filterstrings.join( "|" ), "i");

then test if the passedinstring is there

var isAvailable = regex.test( passedinstring ); 

JavaScript replaceAll case-insensitive search using variable rather than a string

Basically, what you want is to create a dynamic regular expression, instead of hardcoding it. This is done with the help of. RegExp constructor, which takes the string representation of a regexp and flags (I messed the string capitalization to demo the preservation of case):

string1 = 'DnA deoxyribonucleic acid'
string2 = 'DNA deoxyribonucleic aCId'

const replacer = (str, replace) => {
const re = new RegExp(`(${replace})`, 'gi')
return str.replaceAll(re, '***$1***')
}

console.log(replacer(string1, 'dna'))
console.log(replacer(string2, 'acid'))

Regex: ignore case sensitivity

Assuming you want the whole regex to ignore case, you should look for the i flag. Nearly all regex engines support it:

/G[a-b].*/i

string.match("G[a-b].*", "i")

Check the documentation for your language/platform/tool to find how the matching modes are specified.

If you want only part of the regex to be case insensitive (as my original answer presumed), then you have two options:

  1. Use the (?i) and [optionally] (?-i) mode modifiers:

    (?i)G[a-b](?-i).*
  2. Put all the variations (i.e. lowercase and uppercase) in the regex - useful if mode modifiers are not supported:

    [gG][a-bA-B].*

One last note: if you're dealing with Unicode characters besides ASCII, check whether or not your regex engine properly supports them.

How to define the 'case insensitive' modifier i in a regex?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

new RegExp(res, 'i')

Is what you need.



Related Topics



Leave a reply



Submit