1. Home
  2. Computing & Technology
  3. JavaScript

Regular Expressions

18. Multiline Mode

clr gif

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.

Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.