How to quickly learn regular expressions _ regular expressions

Source: Internet
Author: User

The concept of regular expressions

Regular expressions, also known as formal representations, general representations (English: Regular Expression, often abbreviated as regex, RegExp, or re) in code, a concept of computer science. A regular expression uses a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern.

In daily work, we often write regular expressions, such as in the form is often used to verify that the user input format is correct, we will use regular expressions; You can see the regular expression as a language that can describe the problem, which is designed specifically for pattern matching.

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

Syntax for regular expressions

It's easy to be rude to interpret regular expressions as looking for things, for example: to find the word "world" in "Hello World", first describing it in plain language is to look for "letter combinations from W to D", using regular to describe \bw\w*d\b, This is the pattern string, which consists of a meta character and a literal character

• Common meta characters are as follows:

• Flags in regular expressions

• Escape characters in regular expressions


Regular Expressions in JS

• Two ways to generate

Example: Match 0-9 has not appeared

Copy Code code as follows:

1, through REGEXP var reg=new RegExp (' ^[0-9] ', ' g ');
2, through the expression literal amount of Var reg=/^[0-9]/g;

• Two commonly used methods

Test (), exec ()

Copy Code code as follows:

1. Test method: If the input string matches the pattern, test returns True, no returns false
2. Exec method: If the input string matches the pattern, an array is returned and NULL is returned without a match

/*test () method to match whether there appears 0-9*/
var reg1=/^[0-9]/;
Reg1.test (' There is no number ');
Returns the result is False
/*exec () method, matches the word inside the sentence, two words a group
///* did not set the global flag * * * *
var reg2=/(\w+) \s (\w+)/;
Reg2.test (' There is no number ');
Returns the result [' There is ', ' There ', ' is ']
//The No. 0 element is a matching string, the following two elements are the substring of the grouped reference/
* SET global flag
/var reg3=/(\w+) \s (\w+ )/g;
Reg3.test (' There is no number ');
If the global flag is set, EXEC () will cycle through
//Find the result first [' There is ', ' There ', ' is '], second result [' No number ', ' no ', ' no ', ' number '], third time result null 
The exec () method if the global flag is set, it must be manually set to Reg.lastindex = 0 in the loop, otherwise it will interval match

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

String objects and regular expressions

• There are many methods of using regular expression objects as parameters in string objects

Property Description
Match (RegExp) Returns the matching result of the regular expression regexp
Replace (Searchvalue,replacevalue) Will Searchvalue (regular expression or string value)
Replace with Replacevalue and return a string of responses
Search (RegExp) Returns the subscript for the regexp of the regular expression, and returns-1 if there is no match
Split (Separator,limit) Separator by parameter (string or regular expression)
Splits the string and returns an array of strings

The match method returns an array of strings that match the element to the pattern, and when set global flags, all string arrays that match the pattern are returned without setting the same as the Exec method

application Example:

var text= ' abc def ghi JKL ';
Setting global flag
Text.match (/\w+/g);
["ABC", "Def", "Ghi", "JKL"]
//Do not set global flag
Text.match (/(\w+) \s (\w+)/);
["ABC def", "abc", "Def"] Replace method returns the replaced string, if the global flag is set, replaces all matching strings, otherwise only the first matching string is replaced; if the first argument is grouped in the Replace, The second parameter is able to identify the forward reference of a group by means of a symbol

application Example:

var text= "abc def ghi JKL";
Replace the space with the character
Text.replace (/\s/, ', ');
"Abc,def ghi jkl"
text.replace (/\s/g, ', ');
"ABC,DEF,GHI,JKL"
//grouping characters before spaces, replacing them with commas and moving forward one
text.replace (/(.) \s/g, ", $");
"AB,CDE,FGH,IGKL" • In Search and split methods, the global flag of the regular expression does not work

The above content is small series to introduce the Quick learning regular expression method, hope to help everyone!

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.