`
leonzhx
  • 浏览: 796669 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Chapter 8. Regular Expressions

阅读更多

1.  Regular expression patterns are written between slashes (/). Slashes inside the expression have to be escaped with backslashes (but quotes do not).

 

2.  The search method of strings works like indexOf—it returns the position at which it finds its argument—but takes a regular expression instead of a string.

 

3.  The [ and ] characters have a special meaning inside a regular expression. They enclose a list of characters and will match when one of these characters is found.

 

4.  The dot (.) can be used to mean “any character that is not a line-break character.” An escaped d (\d) means “any digit.” An escaped w (\w) matches any “word” character, meaning alphabetic characters, digits, and the underscore character. An escaped s (\s) matches any whitespace character (things such as tabs, newlines, and spaces).

 

5.  You can replace the \d, \w, and \s characters with capital letters to negate their meanings. When using [ and ], a pattern can be inverted by starting with a ^ character.

 

6.  The ^ character matches the start of the string, and the $ character matches the end.

 

7.  Regular expressions are objects and have methods. The test method returns a Boolean indicating whether the given string matches(contains) the expression.

 

8.  The \b escape character matches a “word boundary,” which can be punctuation, whitespace, or the start or end of a string.

 

9.  Putting an asterisk (*) after an element allows it to be repeated any number of times, including zero. A plus (+) does the same but requires the pattern to occur at least one time. A question mark (?) makes an element “optional”—it can occur zero or one time.

 

10.  A number between braces ({4}) gives the exact number of times that element must occur. Two numbers with a comma between them ({3,10}) indicate that the pattern must occur at least as often as the first number and at most as often as the second one. Analogously, {2,} means two or more occurrences, while {,4} means four or less.

 

11.  It is possible to group parts of a regular expression together with parentheses and then do something with the whole group.

 

12.  After the closing slash, “options” may be added to a regular expression. The option i means that the expression is case-insensitive.

 

13.  Strings have a method named match, which takes a regular expression as an argument. It returns null if the match failed and returns an array of matched strings if it succeeded. The first element in the returned array is always the part of the string that matched the whole pattern. When there are parenthesized parts in the pattern, the parts they match are also added to the array.

 

14.The replace method of string values can be given a regular expression as its first argument. Option g stands for “global” and means that every part of the string that matches the pattern should be replaced.

 

15.  Sometimes we need to keep parts of the strings we replace. The $1 and $2 in the replacement string refer to the parenthesized parts in the pattern. $1 is replaced by the text that matched against the first pair of parentheses, $2 by the second, and so on, up to $9.

 

16.  When the second argument given to the replace method is a function value instead of a string, this function is called every time a match is found, and the matched text is replaced by whatever the function returns. The arguments given to the function are the matched elements, similar to the values found in the arrays returned by match: The first argument is the whole match, and after that there is an argument for every parenthesized part of the pattern.

 

17.  The first argument to the RegExp constructor is a string containing the pattern, and the second argument (which may be omitted) can be used to add case-insensitivity or globalness.

 

18.  Any backslashes that must end up in the regular expression itself have to be escaped.

 

19.  The split method of strings also allows a regular expression as its argument.

 

分享到:
评论

相关推荐

    Jeffrey E. F. Friedl - Mastering.Regular.Expressions.3rd.Edition

    If you think you know all you need to know about regularexpressions, this book is a stunning eye-opener. As this book shows, a command of regular expressions is an invaluable skill. Regular ...

    JavaScript The Good Parts

    Regular Expressions Chapter 8. Methods Chapter 9. Style Chapter 10 Beautiful Features Appendix A. Awful Parts Appendix B. Bad Parts Appendix C. JSLint Appendix D. Syntax Diagrams Appendix E.JSON ...

    Learn.C++.Programming.Language.Become.A.Complete.C++.Programmer.pdf

    Chapter 8. Structures, Unions, and Enumerations Chapter 9. Statements Chapter 10. Expressions Chapter 11. Select Operations Chapter 12. Functions . 305 Chapter 13. Exception Handling Chapter 14. ...

    Speaking JavaScript

    Chapter 8. Values Chapter 9. Operators Chapter 10. Booleans Chapter 11. Numbers Chapter 12. Strings Chapter 13. Statements Chapter 14. Exception Handling Chapter 15. Functions Chapter 16. Variables: ...

    javascript语言精粹(中英文版)

    Section A.8. + Section A.9. Floating Point Section A.10. NaN Section A.11. Phony Arrays Section A.12. Falsy Values Section A.13. hasOwnProperty Section A.14. Object Appendix B. Bad Parts ...

    Modern.Perl.4th.Ed

    Regular Expressions and Matching Chapter 7. Objects Chapter 8. Style and Efficacy Chapter 9. Managing Real Programs Chapter 10. Perl Beyond Syntax Chapter 11. What to Avoid Chapter 12. Next Steps ...

    Python_Regular_Expressions_-_Learn_Python_Regular_Expressions_in_an_Easy_Way.pdf.pdf

    在"Chapter1: Introduction to Regular expressions"部分,介绍了正则表达式的历史,以及与之相关的一些重要概念。读者可以了解到正则表达式的起源和发展,以及如何将正则表达式用于各种文本处理任务。正则表达式的...

    Automate.the.Boring.Stuff.with.Python.Practical.Programming

    Pattern Matching with Regular Expressions Chapter 8. Reading and Writing Files Chapter 9. Organizing Files Chapter 10. Debugging Chapter 11. Web Scraping Chapter 12. Working with Excel Spreadsheets ...

    C# Programming Cookbook [EPUB+MOBI+PDF]

    Regular Expressions Chapter 10. Choosing and Using a Source Control Strategy Chapter 11. Creating a Mobile Application in Visual Studio Chapter 12. Writing Secure Code and Debugging in Visual Studio ...

    High Performance Python

    Chapter 1. Introduction, Section 1.1. The High Performance Buzz-word, Chapter 2. The Theory of Computation, Section 2.1. Introduction, Section 2.2. Problems, Section 2.3.... Limit Regular Expressions

    Perl Debugged.pdf

    Chapter 1. Introduction.............................................................................................................1 1.1 Reality.........................................................

    Professional C#4.0 and .NET 4.0 part2

    Chapter 9: Strings and Regular Expressions. . . . . . . . . . . . . . . . . . . . .207 Chapter 10: Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225 Chapter 11: Language ...

    Professional C# 4.0 and .NET 4.0 part1

    Chapter 9: Strings and Regular Expressions. . . . . . . . . . . . . . . . . . . . .207 Chapter 10: Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225 Chapter 11: Language ...

Global site tag (gtag.js) - Google Analytics