What can regular expressions be used for?
This article mainly introduces what regular expressions can be used for, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.
Regular expression, also known as regular representation, conventional representation (English: Regular Expression, often abbreviated to regex, regexp or RE in code), is a concept in computer science. Regular expressions use a single string to describe and match a series of strings that conform to a syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that matches a certain pattern.
What can regular expressions do?
1, data validity verification, you can test a string to see if the string conforms to a certain specification.
2, replace the text, you can delete the specified text in the document, or replace it with other text.
3. Extract a substring from the string and use it to find specific text in the text or input field.
Regular expression syntax
A regular expression is a text pattern consisting of ordinary characters (amurz) and special characters (metacharacters), which acts as a template to match a character pattern with the searched string.
Metacharacter
A special character with a special meaning.
Basic metacharacter
. Matches any character except line breaks. For example, regular expression r.t can match "rat, rut, r t", but not root.
[] match matches a character that appears in []
| | or sensitive word ab | cd | ed | df |
() increase priority a (bc) to realize grouping
Qualified metacharacter
* repeat zero or more times
+ repeat one or more times
? Repeat zero or once
{n} repeat n times
{n,} repeat n or more times
{n ~ m} repeat n to m times
Leading and trailing metacharacter
^ match string start for example the regular expression ^ when can match the beginning of "when in the" but not "what and when in the"
The end of the matching string, for example, the regular expression food$ can match to the end of "he's food"
Abbreviated form
\ b the beginning or end of a word
\ d numbers that match 0-9\ D non-digits
\ s any white space character includes tab and newline character\ S all characters except for uppercase non-white space
\ W matches letters, numbers or underscores\ W is not alphanumeric underscores
Escape character
If you want to find the metacharacter itself, for example, if you want to find it. Or * there will be a problem because it will be interpreted as something else. At this point, you need to use\ to cancel the special meaning of these characters.
So you need to use\. \ * find\ itself should write\\
Using regular expressions in javascript
Create a regular expression
/ / literal value var reg = /\ dmarker / constructor var regx = new RegExp (\\ d+)
Match
If (reg.test ("45646515")) {alert ("OK");} if (regx.test ("sd")) {alert ("ok")}
Extract
Var str= "853020304@qq.com"; var reg = / ([a-zA-Z0-9] +) @ ([a-zA-Z0-9] + (\. [a-zA-Z0-9] +) /; var match = reg.exec (str); alert (match [0]); / / 853020304@qq.com matching object alert (match [1]); / / 853020304 [1] [2] is each group alert (match [2]) / / qq.comalert (match [3]); / / .com
Cyclic extraction
Var str = "12345"; var r = /\ d\ d r.exec g; / / g global global matching var m = r.exec (str); / / 12alert (m); m = r.exec (str); / / 34alert (m); m = r.exec (str); / / null no return nullalert (m); var arr = []; var m = null While ((m=r.exec (str))! = null) / / if not equal to null continue loop {arr.push (m); / / add to array} alert (arr.length); / / [0] = 12 [1] = 34
Replace
String str = .replace (regular expression or string, replace with a string); var str = "ab--c--d--e--f-g"; var result = str.replace (/-+ / gjagjinghe'); / / replace only the first alert (result) if you don't write g; / / ab,c,d,e,f,gvar date = "August 24th, 2015" Var result = date.replace (/ (\ d +) year (\ d +) month (\ d +) /, "$1m / m 2m / m 3"); alert (result); / / 2015-8-24
Using regular expressions in c
Var regx = "^ [a-zA-Z0-9] {6Jing 20} $"; if (! Regex.IsMatch ("abcdef;sd123", regex) {/ / length must be 6-20, letters and numbers}
Common expression
Match ID:\ d {15} |\ d {18}
Match the postcode of China: [1-9]\ d {5} (?!\ d)
Match Tencent QQ number: [1-9] [0-9] {4,}
Match domestic phone number:\ d {3} -\ d {8} |\ d {4} -\ d {7}
Whether the matching account is legal (5-16 bytes are allowed at the beginning of the letter, and alphanumeric underscores are allowed): ^ [a-zA-Z] [a-zA-Z0-9 _] {4j 15} $
Regular expression that matches the URL URL: [a-zA-z] +: / / [^\ s] *
The regular expression that matches the Email address:\ W + ([- +.]\ w +) * @\ w + ([-.]\ w +) *.\ w + ([-.]\ w +) *
Regular expression that matches the leading and trailing white space characters: ^\ s* |\ sblank $
Regular expression that matches the HTML tag:] * >. *? |
Regular expressions that match Chinese characters: [\ u4e00 -\ u9fa5]
Restrict the input of text boxes in web forms
You can only enter Chinese:
Only numbers can be entered:
You can only enter numbers and English:
Thank you for reading this article carefully. I hope the article "what can regular expressions be used for?" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!