Regular Expressions18. Multiline Mode |
|
Join the DiscussionMore of this FeatureIntroduction Pattern Matching Ignore Case and Global Replacing Matches Spliting Matches Start and End Meta Characters Character Classes Repetitions Greedy, Reluctant, etc. Predefined Classes Special Characters ASCII and Unicode Boundaries Grouping Back References Non-Capturing Groups Lookahead All of the regular expressions we have looked at so far will process the text that we are comparing the expression to as if it were a single line. This will work fine provided that we do not need to match patterns against line boundaries. When we do need to match patterns against line boundaries we need to switch our regular expression into multiline mode. We do this by adding an indicator after the expression the same way we do for ignorecase and global.
var re = /^deal$/mg
With this example the "m" switches the regular expression into multiline mode. This changes the meanings of the "^" and "$" boundary conditions. Now instead of indicating just the start and end of the complete text (so that our example would match only if the complete text was "deal") these boundary conditions in multiline mode represent the start and end respectively of any line. The example expression will therefore match any line within the text that consists just of the word "deal". Of course you don't need to test both start and end of line in your expression in multiline mode, this mode can be used equally well to find lines that just start or end with a particular pattern. |

