
-----------------------------------
wtd
Tue Nov 16, 2004 11:45 pm

[Regex-tut] Case insensitivity and local modifiers
-----------------------------------
Quick recap

In previous tutorials I used a set to achieve case insensitivity at the beginning on a word.

/[hH]ello/

It works, but...

This works, and it's even fairly concise, but, it doesn't really indicate that case insensitivity was my goal.  It also gets messier if, for instance, I wanted to make the entire word "hello" case-insensitive.

/[hH][eE][lL][lL][oO]/

There's a better way?

Yes, there is.  The "i" modifier can make the entire regular expression case-insensitive.  It's applied in the same way as the m and x modifiers.

/hello/i

Back to the future

Going back to the original example, we want only the first letter of "hello" to be case-insensitive.  Fortunately, we can apply modifiers locally.

/(?i:h)ello/

Similarly, modifiers can be negated locally.  A rather odd reversal of the above might look like:

/h(?-1:ello)/i

These two styles can be mixed.

/(?i-x:h) ello /x
