Regular Expressions (easy to understand) and regular expressions are easy to understand

Source: Internet
Author: User

Regular Expressions (easy to understand) and regular expressions are easy to understand

What is a regular expression:

In short, regular expressions are a powerful tool for pattern matching and replacement. Trace Regular Expressions in almost all UNIX/LINUX-based software tools, such as Perl or PHP scripting languages. In addition, the script language of the JavaScript client also provides support for regular expressions. Now, regular expressions have become a common concept and tool and are widely used by various technical personnel.

Basic regular expression syntax

1. Create a JavaScript Regular Expression (RegExp) object

Var reg = RegExp ('s '); var reg =/s/; // It is recommended to use the abbreviated method. It cannot be empty. Otherwise, it will be considered as a comment.

Ii. predefine characters

First, let's take a look at some symbolic meanings to help you quickly understand the following examples.

\ S: Space
\ S: non-space
\ D: Number
\ D: Non-numeric
\ W: character
\ W: Non-Character
\ I: case insensitive
\ G: Global match (generally, if the first regular expression matches the condition, it will stop. If this regular expression matches the condition, it will tell the regular expression not to stop)
|: Or
.: Any character
\ B: independent part (START, end, space)
\ B: non-independent part
\ N: A duplicate subitem, for example

Var reg =/(a) (B) (c) \ 1/; // => abca repeats subitem

To use a real symbol such as ".", you only need to add \, such \.

Iii. Common Methods

1. test () => Search for the content specified by the regular expression in the string. If yes, true is returned. Otherwise, false is returned.

// Usage: regular. test (string) var data = '20140901'; var reg =/\ d/; // \ d indicates the number if (reg. test (str) {console. log ('Number content '); console. log (reg. test (str) // return true}

2. match () => query the content specified in the Regular Expression in the string. If the result is successful, the returned content (in array format) is returned. Otherwise, null is returned.

// Usage: string. match (regular) var data = '123456mple789mple875654'; var reg =/mple/gi; console. log (data. match (reg); // mple, Mple

3. search () => query the content specified by the regular expression in the string. if the content is found successfully, the position of the current content starts from 0. (if more than one content meets the regular expression condition, returns the first location found). If not, returns-1.

// Usage: String. search (regular) var data = '1234mple56789mple987mple654321 '; var reg =/mple/gi; console. log (data. search (reg); // 4

4. replace () => query the content specified by the regular expression in the string. If found, replace the content and return the content after replacement.

// Usage: String. replace (Regular Expression, New String/callback function) var data = 'lala ~ A lot of La la; var reg =/La/g; var rep = data. replace (reg, function (data) {var ne = ''; for (var I = 0; I <data. length; I ++) {ne + = '*';} return ne ;}); console. log (rep );

5. exec () => find and return the current matching result, and return it as an array

// Usage: Zheng ze.exe c (string) var data = "1234mple5678mple99876mple543Mple21"; var reg =/mple/ig; var s = reg.exe c (data) console. log (s. index); // 4

6. split () = regular separator string

7. sort (): sorting method in the array by ACALL code

8. join (): The method in the array. Convert the array to a string.

var data = '4445554654123156489151321456';var arr = data.split('');data = arr.sort().join('');console.log(data);

Iv. Common quantifiers

{N, m}: appears at least n times, at most m times

{N ,}: at least n times

*: Any time is equivalent to {0 ,}

? : Zero or one time is equivalent to {0, 1}

+: The search result appears at least once or any time {1 ,}

{N}: Exactly n times

Finally, let's give an example to understand and determine the QQ number.

HTML

<Input class = "qq" type = "text" placeholder = "Please enter the qq number"> <button type = "button" class = "sub"> detection </button>

Regular Expression

// First, let's take a look at the nature of our daily logon QQ number. 1. The first one is definitely not 0. 2. It must be a 5-10 digit number. var oInput = document. querySelector ('. qq '); var oSub = document. querySelector ('. sub '); var reg =/^ [1-9] \ d {4, 9} $/; // to prevent the last few digits from appearing in the case of letters like abc, therefore, you need to add $ to limit the number of tails. The final logic is as follows: the first is 0-9, and then the fourth-9 digit. OSub. onclick = function () {if (reg. test (oInput. value) {alert ('detected successfully');} else {alert ('account not exist ');}};

The above is a regular expression (simple and easy to understand) introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.