Normally, matching modes are specified outside the regular expression. In a programming language, you pass them as a flag to the regex constructor or append them to the regex literal. In an application, you’d toggle the appropriate buttons or checkboxes. You can find the specifics in the tools and languages section of this website.
Sometimes, the tool or language does not provide the ability to specify matching options. The handy String.matches() method in Java does not take a parameter for matching options like Pattern.compile() does. Or, the regex flavor may support matching modes that aren’t exposed as external flags. The regex functions in R have ignore.case as their only option, even though the underlying PCRE library has more matching modes than any other discussed in this tutorial.
In those situations, you can add the following mode modifiers to the start of the regex. To specify multiple modes, simply put them together as in (?ismx). All regex flavors discussed in this tutorial support mode modifiers at the start of the regex, except for JavaScript, RE2, std::regex, Oracle, XML Schema, and XPath POSIX do not support mode modifiers at all. Boost supports mode modifiers when you select its default ECMAScript grammar but not with its other grammars.
The following modifier letters are supported by the regex flavors discussed in this tutorial. Some like (?i) are universal. Others are only supported by specific flavors. A few letters are listed more than once because they have completely different functions in different regex flavors. The list organizes the letters by related functions, with increasing specialty as you go down the list.
All these modes can be turned off by preceding them with a hyphen. (?ix-sm) turns on case insensitivity and free-spacing and turns off the other two options. All flavors that support mode modifiers at the start of the regex support turning off options, except for Tcl and Python.
The following mode modifiers can only be turned on unless a variant with a hyphen is explicitly mentioned in the list. Python only allows them at the very start of the regex. You cannot use both (?a) and (?u) in the same modifier. (?ua) and (?au) are errors in Perl and Python. In Ruby only the last letter takes effect.
If you insert a mode modifier such as (?ism) in the middle of the regex then the modifier only applies to the part of the regex to the right of the modifier. If you use alternation after the modifier then it applies to all following alternatives as well. If you place the modifier inside a group then it only affects the remainder of that group, including any nested groups that follow the modifier, and any following alternatives within the same group.
Tcl and Python are the only flavors discussed in this tutorial that do not support mode modifiers in the middle of the regex. They are an error in Tcl and in Python 3.7 and later. Python 3.6 and prior allowed modifiers in the middle of the regex but still applied the modifier to the whole regex as if you had placed it at the very start of the regex.
Modifiers in the middle of the regex break alternation that follows the modifier in Ruby. Use a modifier span if you need an option to apply to part of a regex that uses alternation in Ruby.
Instead of using two modifiers, one to turn an option on, and one to turn it off, you use a modifier span. (?i)caseless(?-i)
All flavors that support mode modifiers in the middle of the regex also support modifier spans. Python and Tcl do not support modifier spans even though they support non-capturing groups.
JavaScript makes a surprise appearance here. Originally, JavaScript did not support mode modifiers at all. But ECMAScript 2018 introduced the /u flag. In addition to enabling Unicode features, this flag enables mode modifier spans with the options (?i:case insensitive), (?s:single-line), and (?m:multi-line). As with other flavors, you can combine the letters in a single modifier span and use the hyphen to turn off certain options.
| Quick Start | Tutorial | Search & Replace | Tools & Languages | Examples | Reference |
| Introduction | Table of Contents | Special Characters | Non-Printable Characters | Regex Engine Internals | Character Classes | Character Class Subtraction | Character Class Intersection | Shorthand Character Classes | Dot | Anchors | Word Boundaries | Alternation | Optional Items | Repetition | Grouping & Capturing | Backreferences | Backreferences, part 2 | Named Groups | Relative Backreferences | Branch Reset Groups | Free-Spacing & Comments | Unicode Characters & Properties | Mode Modifiers | Atomic Grouping | Possessive Quantifiers | Lookahead & Lookbehind | Lookaround, part 2 | Lookbehind Limitations | (Non-)Atomic Lookaround | Keep Text out of The Match | Conditionals | Balancing Groups | Recursion and Subroutines | POSIX Bracket Expressions | Zero-Length Matches | Continuing Matches | Backtracking Control Verbs | Control Verb Arguments |
Page URL: https://www.regular-expressions.info/modifiers.html
Page last updated: 10 October 2025
Site last updated: 29 October 2025
Copyright © 2003-2025 Jan Goyvaerts. All rights reserved.