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

10.3 The RegExp Object

10.3 The RegExp Object


As mentioned at the beginning of this chapter, regular expressions are represented as RegExp objects. In addition to the RegExp() constructor, RegExp objects support three methods and a number of properties. RegExp pattern-matching methods and properties are described in the next two sections.

The RegExp() constructor takes one or two string arguments and creates a new RegExp object. The first argument to this constructor is a string that contains the body of the regular expression—the text that would appear within slashes in a regular-expression literal. Note that both string literals and regular expressions use the \ character for escape sequences, so when you pass a regular expression to RegExp() as a string literal, you must replace each \character with \\. The second argument to RegExp()is optional. If supplied, it indicates the regular-expression flags. It should be g, i, m, or a combination of those letters.

10.3 The RegExp Object | 261

For example:

// Find all five-digit numbers in a string. Note the double \\ in this case. var zipcode = new RegExp("\\d{5}", "g");

The RegExp() constructor is useful when a regular expression is being dynamically created and thus cannot be represented with the regular-expression literal syntax. For example, to search for a string entered by the user, a regular expression must be created at runtime with RegExp().

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/27684.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1