How to quickly learn Regular Expressions and quickly learn Regular Expressions

Source: Internet
Author: User

How to quickly learn Regular Expressions and quickly learn Regular Expressions

Regular Expression Concept

Regular Expression, also known as Regular Expression and Regular Expression (English: Regular Expression, often abbreviated as regex, regexp or RE in code), is a concept of computer science. Regular Expressions use a single string to describe and match a series of strings that conform to a certain syntax rule. In many text editors, regular expressions are usually used to retrieve and replace texts that match a certain pattern.

In daily work, we often write regular expressions. For example, regular expressions are often used in forms to verify whether the format entered by users is correct; the regular expression can be regarded as a language that can describe the problem. It is specially designed for pattern matching.

-------------------------------------------------------------------------------

Regular expression syntax

The regular expression can be understood as a query. For example, you need to find the word "World" in "Hello world, to describe in a straightforward language, you can find the "letter combination starting with w and ending with d". To describe using regular expressions, It is \ bw \ w * d \ B. This is the mode string, it consists of metacharacters and area characters

• Common metacharacters are as follows:

• Flag in Regular Expressions

• Escape characters in Regular Expressions


Regular Expressions in JS

• Two generation methods,

Example: Match 0-9

Copy codeThe Code is as follows:
1. Use RegExp var reg = new RegExp ('^ [0-9]', 'G ');
2. Use the expression literal var reg =/^ [0-9]/g;

• Two common methods

Test (), exec ()

Copy codeThe Code is as follows:
1. test method: If the input string matches the pattern, test returns true, and false if not.
2. exec method: If the input string matches the pattern, an array is returned. If there is no matching, null is returned.

/* Test () method, matching whether 0-9 */var reg1 =/^ [0-9]/; reg1.test ('there is no number '); // The returned result is false/* exec (), which matches the words in the sentence, two words in one group * // * No global flag */var reg2 =/(\ w +) \ s (\ w + )/; reg2.test ('there is no number'); // The returned result ['there is ', 'there', 'is'] // 0th elements are matched strings, the following two elements are the sub-strings/* set the global flag */var reg3 =/(\ w +) \ s (\ w +)/g; reg3.test ('there is no number'); // if the global flag is set, exec () will search cyclically // The first query result ['there is ', 'there ', 'Is '], the second result is ['no number', 'No', 'number'], and the third result is null exec () if the global flag is set, you must manually set its reg. lastIndex = 0; otherwise, it will match at intervals

--------------------------------------------------------------------------------

String object and Regular Expression

• Many methods in string objects that use regular expression objects as parameters

Attribute Description
Match (regexp) Returns the matching result of the regular expression regexp.
Replace (searchValue, replaceValue) Set searchValue (regular expression or string value)
ReplaceValue and return the response string
Search (regexp) Returns the subscript of the location where regexp matches the regular expression. If no match is found,-1 is returned.
Split (separator, limit) Use the separator parameter (string or regular expression)
Splits the string and returns an array of strings.

• The match method returns an array of strings matching the element and pattern. When the global flag is set, all string arrays matching the pattern are returned, which is the same as the exec method when not set.

Example:

Var text = 'abc def ghi jkl '; // set the global flag text. match (/\ w +/g); // ["abc", "def", "ghi", "jkl"] // The Global flag text is not set. match (/(\ w +) \ s (\ w +)/); // ["abc def", "abc ", "def"] • The replace method returns the replaced string. If the global flag is set, all matched strings are replaced. Otherwise, only the first matched string is replaced; in replace, if the first parameter uses a group, the second parameter can identify the forward reference of the Group by symbol.

Example:

Var text = "abc def ghi jkl"; // replace spaces with character text. replace (/\ s/, ','); // "abc, def ghi jkl" text. replace (/\ s/g, ','); // "abc, def, ghi, jkl" // grouping of characters before spaces, replace with a comma and move a text forward. replace (/(.) \ s/g, ", $1"); // "AB, cde, fgh, igkl" • In the search and split methods, the global flag of the regular expression does not work

The above content is a small series of methods to quickly learn regular expressions, I hope to help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.