3

I want to know if Javascript RegExp has the ability to turn on and off case insensitivity within the regular expression itself. I know you can set the modifier for the entire regex expression, but that isn't what I'm talking about.

For example, my search string could be:
teXT To seArcH TOP SECRET.

I want to find the insensitive case part "text to search" and then force case sensitivity for "TOP SECRET".

Thus, this wouldn't match (because of case sensitivity for top secret):
teXT To seArcH Top Secret

but this would match (first section case doesn't matter):
text to search TOP SECRET

In Perl, you can do this within the search string. Does Javascript's regular expression engine support anything like this?

2
  • You could do it with a insensitive regex search followed by a small test. Or by writing some code to make the regex for you. Commented May 24, 2013 at 19:10
  • 1
    No. JavaScript doesn't have scoped modifiers like (?i:...). You pretty much have to do it yourself /[tT][eE][xX][tT] TOP SECRET/, or break your test in two: var m = /text TOP SECRET$/i.match(s); m = m && m[0].match(/TOP_SECRET$/). Commented May 24, 2013 at 19:12

5 Answers 5

4

You can write the RegExp in case-sensitive "longhand"

/[tT][eE][xX][tT] [tT][oO] [sS][eE][aA][rR][cC][hH] TOP SECRET/
    .test('text to search TOP SECRET');
// true

An alternative approach is two regular expressions, an insensitive one followed by a strict one

function test(str) {
    var m = str.match(/text to search (TOP SECRET)/i);
    return (m || false) && /TOP SECRET$/.test(m[1]);
}

test('text to search TOP SECRET'); // true
test('text to search ToP SECRET'); // false

Further, function test above can be optimised in this specific case (as the TOP SECRET part is effectively a string literal which will always have exactly the same form), it doesn't require a second full RegExp test to check it, i.e. you can do

(m || false) && 'TOP SECRET' === m[1];
Sign up to request clarification or add additional context in comments.

Comments

3

What you can do is build your regex like this :

var str = "teXT To seArch";
var r = new RegExp(
  str.split('').map(function(c){return '['+c.toLowerCase()+c.toUpperCase()+']'}).join('')
  + ' TOP SECRET'
);

1 Comment

+1-ing this as it is the most helpful by way of programmatically creating a RegExp for the desired type of test.
3

According to this reference page, Javascript supports:

No mode modifiers to set matching options within the regular expression.

So the answer is no, you cannot turn case sensitivity on or off within the regular expression.

Comments

2

Don't think so. I'd suggest doing this:

var m;
var str = "teXT To seArcH TOP SECRET";
if ((m = str.match(/text to search (top secret)/i)) && m[1].match(/TOP SECRET/)) {
  // matched.
}

Comments

0

Why not simply use 2 Regex's?

var str = "teXT To seArcH TOP SECRET";
if (/text to search/i.test(str) && /TOP SECRET/.test(str)) {
  console.log('match!');
}

I think there is rarely a good reason to make a single very complex regex to cover every case, when you could instead make a handful of Regex's which you combine with code logic. I'd much rather have small bite size Regex's from a maintenance point of view, as figuring what a complex regex actually does can be quite daunting.

1 Comment

The order of the text is not preserved, not mentioning the case where you want to search for /(?i:text to SEARCH) char/, and the string is tExt to searchar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.