JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

10.1.6 Flags

10.1.6 Flags


There is one final element of regular-expression grammar. Regular-expression flags specify high-level pattern-matching rules. Unlike the rest of regular-expression syntax, flags are specified outside the /characters; instead of appearing within the slashes, they appear following the second slash. JavaScript supports three flags. The i flag specifies that pattern matching should be case-insensitive. The g flag specifies that pattern matching should be global—that is, all matches within the searched string should be found. The m flag performs pattern matching in multiline mode. In this mode, if the string to be searched contains newlines, the ^ and $ anchors match the beginning and end of a line in addition to matching the beginning and end of a string. For example, the pattern /java$/im matches “java” as well as “Java\nis fun”.

These flags may be specified in any combination. For example, to do a case-insensitive search for the first occurrence of the word “java” (or “Java”, “JAVA”, etc.), you can use the case-insensitive regular expression /\bjava\b/i. And to find all occurrences of the word in a string, you can add the g flag: /\bjava\b/gi.

Table 10-6 summarizes these regular-expression flags. Note that you’ll see more about the gflag later in this chapter, when the String and RegExp methods are used to actually perform matches.

Table 10-6. Regular-expression flags

Character Meaning
i Perform case-insensitive matching.
g Perform a global match—that is, find all matches rather than stopping after the first match.
m Multiline mode. ^matches beginning of line or beginning of string, and $matches end of line or end of string.
欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31397.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1