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

3.2.4 Pattern Matching

JavaScript defines a RegExp() constructor for creating objects that represent textual
patterns. These patterns are described with regular expressions, and JavaScript adopts
Perl’s syntax for regular expressions. Both strings and RegExp objects have methods
for performing pattern matching and search-and-replace operations using regular
expressions.
RegExps are not one of the fundamental types of JavaScript. Like Dates, they are simply
a specialized kind of object, with a useful API. The regular expression grammar is com-
plex and the API is nontrivial. They are documented in detail in Chapter 10. Because
RegExps are powerful and commonly used for text processing, however, this section
provides a brief overview.
Although RegExps are not one of the fundamental data types in the language, they do
have a literal syntax and can be encoded directly into JavaScript programs. Text be-
tween a pair of slashes constitutes a regular expression literal. The second slash in the
pair can also be followed by one or more letters, which modify the meaning of the
pattern. For example:
/^HTML/              // Match the letters H T M L at the start of a string
/[1-9][0-9]*/        // Match a non-zero digit, followed by any # of digits
/\bjavascript\b/i    // Match "javascript" as a word, case-insensitive
RegExp objects define a number of useful methods, and strings also have methods that
accept RegExp arguments. For example:
var text = "testing: 1, 2, 3";   // Sample text
var pattern = /\d+/g             // Matches all instances of one or more digits
pattern.test(text)               // => true: a match exists
text.search(pattern)             // => 9: position of first match

text.match(pattern)              // => ["1", "2", "3"]: array of all matches

text.replace(pattern, "#");      // => "testing: #, #, #"
text.split(/\D+/);               // => ["","1","2","3"]: split on non-digits

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