views:

197

answers:

3

Does the JavaScript regex standard support forcing case when doing a search/replace?

I am generally aware of the \u etc. options for forcing the case of a capture group in the replacement portion of a regular expression. I am unable to figure out how to do this in a JavaScript regex though. I do not have access to the JavaScript code itself but rather a program I am using allows entry of the regex strings which it passes through to the regex engine itself.

A: 

JavaScript regular expressions are case sensitive by default and support case insensitivity if passed the /i flag.

Asaph
That would be for the search side of things, not for changing the case of a capture group when replacing. Thanks for answering though.
altendky
+1  A: 

If I understand you correctly, no.

You cannot evaluate javascript (to transform matches to uppercase) from within a regex pattern.

I'm only aware of str.replace() to convert to uppercase.

zen
Thanks for reading the question relatively thoroughly. Note that some other regex engines are capable of changing the case of capture groups when doing the replacement. In other words, I am not trying to inject JavaScript into the regex.
altendky
You're welcome. I understood what you meant when you mentioned \u. Do I win? :)
zen
A: 

String Matching is done using regular expressions.String Replacement, however, only cares about what portion of the subject string you are replacing (which it is fed by the regular expression match), and it just does direct string replacement based on what you give it:

var subject = "This is a subject string";

// fails, case-sensitive match by default
subject.replace(/this/, "That"); 

// succeeds, case-insensitive expression using /i modifier, replaces the word "This" with the word "That"
subject.replace(/this/i, "That");

Now, if you wanted to capture a part of the matched string and use it to change the case, you can do that as well using expression groups (parentheses in your expression):

var subject = "This is a subject string";
var matches = subject.match(/(subject) string/i);
if (matches.length > 0)
{
    // matches[0] is the entire match, or "subject string"
    // matches[1] is the first group match, or "subject"
    subject.replace(matches[1], matches[1].toUpperCase());
    // subject now reads "This is a SUBJECT string"
}

In short, doing a match you can handle case-sensitivity if you wish. Doing a replacement is as simple as telling it what direct string to use for replacement.

localshred
I don't think this is what altendky was looking for. But there is a better way to do what you described here. The replace parameter to String.replace can be a function used to modify each of the matches. //The following example upcases all words starting with T (case insensitive)"The ticking clock was taken down".replace(/(T\w*)/gi, function(match){ return match.toUpperCase();})
Juan Mendes
Definitely dig the anon function as a much smoother approach to my example. Not sure why what I described wasn't what he was looking for, but it doesn't matter anyways, since he already marked the other answer. Thanks for clarifying a better way though!
localshred
You explained how to do something with code which I said I do not have access to. Granted, I really do have access but that is not the problem I was trying to solve. I was hoping for a way to allow a user (myself to start with) to enter a replacement regex which includes the \u (and similar) tokens to change the case of capture groups.
altendky