How to use regular expressions correctly
This article is about how to use regular expressions correctly. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
A regular expression (Regular Expression) is a text pattern that includes normal characters (for example, letters between an and z) and special characters (called metacharacters).
Regular expressions use a single string to describe and match a series of strings that match a syntactic rule.
The following is an example code to introduce the use of regular expressions
/ / A data type, record text rules, some pre-defined special characters, string filtering logic
/ / form verification account length, letter or number combination, advanced search
/ / Features, not only js; minimalist way to manipulate strings; flexible, powerful
/ / A complete collection of regular expressions
Var patt1=new RegExp ("e"); [xss_clean] (patt1.test ("The best things in life are free"))
/ * is there a decimal * /
Function isDecimal (strValue) {var objRegExp= / ^\ d +\.\ d return objRegExp.test (strValue);}
/ * verify whether the Chinese name is composed * /
Function ischina (str) {var reg=/ ^ [\ u4E00 -\ u9FA5] {2pm 4} $/; / * define the validation expression * / return reg.test (str); / * validate * /}
/ * check whether it consists of all 8 digits * /
Function isStudentNo (str) {var reg=/ ^ [0-9] {8} $/; / * define the validation expression * / return reg.test (str); / * validate * /} var arr = new Array (); var reg= new RegExp (/\ d /)
/ / indicates a matching number
Reg = reg.test ("jndwjdw"); console.log (reg)
/ / regular object. Test (string that needs to be verified)
/ / literal declaration
Var reg2 = / male | female /
/ / is there one of them?
Var reg = reg2.test ("Youyou, my girl"); console.log (reg); console.log (/ male | female / .test ("Hadihadiha"))
/ / grammar
/ / .test (); regular expression detects a string
/ / predefined class
Console.log (/. / .test ("\ r")); / / any character console.log (/\ d/.test ("123") except carriage return and line feed; / / check whether it is numeric console.log (/\ D/.test ("sh")); / / non-numeric character, true as long as it is not a number, as long as there is one is true console.log (/\ s/.test ("\ r")) / / true\ r\ n\ f\ h\ two empty strings concatenated as false console.log (/\ S/.test ("ah7")); / / as long as there are visible characters, console.log (/\ w/.test ("bmer8")); / / all alphanumeric and lower short _, as long as there are short console.log (/\ W/.test (("bg8-@") / / true// is correct as long as there are non-word characters.
/ / Custom class, no\
Console.log (/ d/.test ("123")); / / check if there is the word d in this paragraph / / console.log (/ yskma/.test (" yskmama ")); / / true// detects that the string packet does not contain yskma, cannot be disconnected and cannot be interspersed with other console.log (/ yskmama/.test (" ysk ")); / / false / / or priority console.log (/ ysk | mama/.test (" ysk ")) / / true console.log (/ ys (k | m) a/.test ("ysk")); / / false// detects whether there is yska or ysma console.log (/ ys (k | m) a/.test ("yskma")) in this string; / / false// detects whether there is yska or ysma in this string
/ / simple class [] represents a character
Console.log (/ [abc] / .test ("gbhigf")); / / true// whenever any one of the abc appears is console.log (/ y [ABC] z/.test ("yaz")); / / true// contains yz and choose a console.log (/ y [ABC] z/.test ("yabcz")); / / false//abc can only have one
/ / negative class [^] is reversed
Console.log (/ [^ abc] / .test ("a")); / / false// is right and wrong except for parentheses, and cannot have console.log (/ [^ abc] / .test ("af")) in parentheses; / / true// is right and wrong except parentheses, and there can be no parentheses
/ / scope class
Console.log (/ [0-9] / .test ("b8jg")); / / true
/ / Composite class, combined to be a composite class, custom class
/ / ^ boundary to. The beginning
/ / $to. End
/ / ^ bc$ strict match
/ /. Other than enter and change the line
Console.log (/ ^ 6a/.test ("6akjjkak")); / / true console.log (/ ^ 6.a/.test ("6akjjkak")); / / false console.log (/ [.] {8} / .test ("6akjjkak")); / / false, the current "." It represents the string ".", so there are no 8 "." here, so false;console.log (/ ac$/.test ("6akjjkakac")); / / true//ac ends with console.log (/ ^ 6a$/.test ("6a")); / / true console.log (/ ^\ d$/.test ("9")); / / true// can only appear console.log (/ ^ abc\ d$/.test ("abc7")) once; / / true// can only appear once.
/ / quantifier:? 0 | | 1 time; + previous > = 1 time; * > = 0 times
Console.log (/ ^ colo*r$/.test ("color")); / / true console.log (/ ^ colo+r$/.test ("color")); / / true console.log (/ ^ colo?r$/.test ("color")); / / true console.log (/ ^ colo?r$/.test ("colr")); / / true console.log (/ ^ colo?r$/.test ("coloor")); / / false console.log (/ ^ colo*r$/.test ("colooooooor")); / / true
/ / quantifier, {} preceding:. {n} n times; {n,} > = n; {n ·m}. > = n..