JS regular expression common usage examples

The example of this paper tells the common usage of JS regular expression. Share to everyone for reference, as follows:

Foreword: Regular Expression, repeatedly, repeatedly, learned and forgot, forgot to learn, this time I plan to put basic things. They are all sorted out, strengthen memory, and it is convenient for the next query.

Before studying regular expressions, you need to master memory these basic concepts:

1. Metacity: (., \ W, \ w, \ d, \ d, \ s , / S, ^, $,)


Meaning Matches any character other than the wrapper. \ S Represents any blank character (newline, tab, space) \ S Matching any non-empty string \ b matches the word boundary, the beginning and end of the word match. \ B Matching a non-word boundary \ D matches a number, etc. The price is [0-9] \ D matches a number, equivalent to [^ 0-9] \ W Match a single character (letter, number or underscore) equivalent to [A-ZA-Z0-9_], for example, / \ w / match “Apple , ‘3’ in ‘A’, “$ 5.28,” in ‘A’, “$ 5.28,” in ‘3’. \ W Match a non-single character. Equivalent to [^ A-ZA-Z0-9_], for example, / \ w / or / [^ a-za-z0-9 _] / match “50%.” In ‘%’. Matching a non-single character. Equivalent to [^ A-ZA-Z0-9_], for example, / \ w / or / [^ a-za-z0-9 _] / match “50%.” In ‘%’. Match the end of the string. For example, / t $ / does not match ‘T’ in “Eater”, but will match ‘T’ in “EAT”. When the QQ number must be 5 digits to 12 digits, you can use: ^ \ d {5, 12} $ What should I do if you want to repeat multiple characters? You can use small brackets to specify sub-expressions (also called packets), then you can specify this The number of repetitions of the expression. EG: (\ D {1, 3} \.) {3} \ d {1, 3} is a simple IP address matching expression. To understand this expression, analyze it in the following sequence: \ D {1, 3} Match 1 to 3 digits, (\ D {1, 3} \.) {3} Match three numbers plus one in English The end of the sentence (this is also this group) repeated 3 times, and finally add one to three digits (\ D {1, 3}).
\ W
The beginning of the matching string is used in [] brackets to represent exclusion, / ^ A / does not match “AN A” in ‘A’ However, it will match ‘a’ in “AN E”.
We found that \ w to increase writing letters the meaning of. 2. Grouping ([], (), {});

represents a collection of characters. Matching square brackets, including escape sequences. You can use the broken number (-) to specify a range of characters. Special symbols such as points (.) And asterisk (*) have no special meaning in a character set. They don’t have to escape, but the escape is also played. For example, [ABCD] and [A-D] are the same. They all match “BRISKET” to ‘b’, and they also match ‘c’ in “City”. /[A-Z.]+/ and /[ww.]+, all characters are matched in “Test.i.ng”.


indicates the range of quantifiers.

3. Modifiers (I, G, M); i ignore cases G M
Execute multi-line match

4. Quantifiers (*,?, +, -, {n, m},? = N,?! = N);

Match one time or multiple times in front of the front. Equivalent to {1,}. ? Matches the front an expression 0 times or 1 time. Equivalent to {0,1}. {N, M} Match N to M times X (? = Y)
* matches any temporal,. * Connect together means any number of characters that do not contain wraps. Equivalent to {0,}

Match ‘x’ just followed by ‘Y’ after ‘x’. This is called forward and sure. For example, / jack (? = SPRAT) / will match ‘Jack’ just followed by ‘sprat’ behind it. / Jack (? = Sprat | frost) / match ‘jack’ just when it follows ‘sprat’ or ‘frost’. But ‘sprat’ and ‘frost’Not part of the matching results.

x (?! Y)

match ‘x’ only if ‘x’ is not followed behind ‘y’, this is called negative look forward. For example, / \ d + (?! \.) / Matches a number only when there is no time with this number behind the decimal point. Regex /\d+(?!\.)/.exec(“3.141 “) matches ‘141’ but not ‘3.141’

example resolved: (0 \ d {2}?)]? ] “(” and “)” are character element, the latter will be mentioned in the grouping section, where it is required to use escape. The following examples to explain RegExp by: a demand: hi a matching word in a sentence in English. var str = ‘Hi RegExp I love you so much Hi Hi hi’; var reg = new RegExp ( “\\ bhi \\ b”, ” gi “); // g is a modifier, it indicates global matching. \ B is a meta-character, representing a word boundary matching the beginning and end of a word. // amount of direct syntax: rEG2 = / \ \b/gi; console.log(Str.match (REG2 ));//[‘Hi’ ,’Hi’ ,’Hi’ ,’Hi’]// upgrade // Match Hi back Following a Lucyvar strlc = /\bhi@b.*\blucy \B/; Var Luch = ‘Hi Welcome to Beijing Lucy !!!’; console.log (Luch.match (strlc)); Demand 2: Match a Hi word in English in English. VAR REG = / 0 \ D \ D \ D- \ D \ D \ D \ D \ D \ D \ D \ D /; // \ D represents a number, equivalent to [0-9], \ D matches a non-digital character, equivalent to [^ 0-9] var Tel = “0123-887523146”; console.log (TEL.MATCH (REG) )))); // 0123-88752314; // This continuous write multiple times is very stupid, so the variable is introduced. Regtel = /0\d {3 }- \d{8 }/;console.log (tel.match(Regtel) ;//0123-88752314; Demand 3: Write a regular expression of a clear string before and after.
var reg = / \ [- \ d {8} /
This expression can match a telephone number in several formats, like (010) 88886666, or 022-22334455, 02912345678, or the like. We analyze it to make some of it: The first is an escape character \ (which can occur 0 or 1 (), followed by a 0, followed by two numbers (\ d {2}), then?) or – or in a space, it does not appear or appear more than once, the last eight digits (\ d {8}) (?).

String.Prototype.trim = function () {Return this.Replace (/ (^ \ s *) | (\ s * $) / g, “”);} var str2 = “hi space” // There are two spaces here, there are two spaces constole.log (str2.length); // 14console.log (str2.trim() .length); // 8console.log (str2.trim ()); // Hi space

Requirements 4: Match a mailbox.
 VAR EREG = /\s*@@@log (Reg.Test('873619879@qq.com ') // true   
Reference article:

https://www.jb51.net/books/513871. HTML


https: / / www.jb51.net/Article/107796.htm

iPS: Here you offer 2 very convenient regular expression tools for your reference:

JavaScript regular expression online test tool:
   Regular expression online generator: 

more relevant content on JavaScript interested readers can view the site topic: “JavaScript regular expression skills Daquan”, “JavaScript replaced Summary of operation skills, “JavaScript Find Algorithm Skills Summary”, “JavaSCRIPT Data Structure and Algorithm Skill Summary The JavaScript program design is helpful in this article.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment