What is a regular expression
The concept of regular expressions:
A concept in computer science. Regular expressions are often used to retrieve and replace text that conforms to a pattern (rule)
In JS, regular expressions are a class of objects:
typeof /\d+/
"object"
Object.prototype.toString.call(/\d+/);
//"[object RegExp]"
Copy the code
Regular objects have two common properties:
- Flags: Flags for the re object
- LastIndex: the starting point for the next match
How to use regular expressions
How do I create a regular expression
There are two ways to create regular expressions. One is to write the contents of the regular expression directly in //, and the other is to create objects through constructors. Both methods are equivalent.
var reg1=/\d+/;
// Create it directly
var reg2=new RegExp(/\d+/);
// Create a regular object using the constructor
var reg3=new RegExp("\\d+")
// We can also pass in a string as an argument to create a regular object through the constructor, leaving out the double slash.
// And the internal symbols should be escaped
Copy the code
1. Composition of a regular expression
Let’s start with a basic example:
var re=/hello\w+\-? \d*/i; var password=/[a-zA-Z\_\$][0-9a-zA-Z\_\$]*/;Copy the code
From these two expressions, the regular expression is divided into several parts:
①. The content to match
There are several ways to represent what you want to match in a regular expression:
- Give a character directly: like hello above, will match the string “hello”
- Use placeholders: Placeholders can be used to match a class of characters, such as \w in this context, to match letters or numbers
- For special characters, escape: use \- to match – symbols as shown above
- Use [] for range matching: password, for example, matches variable names allowed by JS, and [a-za-z \_\$] represents any character in the range of uppercase and lowercase letters or _ or $, the same as in parentheses
- Use (a | b), means to match a or b, where a and b is not necessarily one character at a time, may also be other kinds of content
The placeholders are divided into the following types:
- \d Matches the number
- \w matches letters or numbers
- \s matches whitespace, space, or TAB, etc
- . Matches any character (except carriage return newline)
②. To match the amount of content
The quantity here refers to how many characters I need to match, such as \w+ for at least one letter or number, \-? Represents 0 or 1 – sign
Quantity can be expressed in the following ways:
- * is any number
- + at least one
- ? Zero or one
- {n} n
- {n, m} n to m
Note that the number is limited to the character next to the number mark, such as /23+/, which matches one 2 and at least one 3. If we want to match any 23, we introduce the concept of a subexpression: we enclose a portion of the regular expression in parentheses, and the quantity constraint that follows it applies to the entire subexpression. For example, /(23)+/ matches 23232323 so there’s at least one 23, not a 2 and a 3
③. Flags of regular expressions
The regular expression flag is used outside the double slash // to further refine the regular expression behavior, as in I above
There are mainly the following signs:
- I: Ignore case
- G: global search, updating the lastIndex property of the re object after each use of exec; Multiple matches can be made using the string match method
- Y: sticky search, that is, the next exec match must start immediately after the last one
- M: Perform multi-line matching
- U: will recognize characters with code points greater than 0xFFFF
- S: dotAll mode, now. Can match carriage return newline character, really match any character
var reg=/\d{1}/;
var regG=/\d{1}/g;
var regY=/\d{1}/y;
var str="0hello1world2";
reg.exec(str)
//["0", index: 0, input: "0hello1world2", groups: undefined]
reg.exec(str)
//["0", index: 0, input: "0hello1world2"RegG. Exec (STR) //[groups: undefined]"0", index: 0, input: "0hello1world2", groups: undefined]
regG.exec(str)
//["1", index: 6, input: "0hello1world2", groups: undefined]
regG.exec(str)
//["2", index: 12, input: "0hello1world2", groups: undefined] regG. Exec (STR) //null: regG."0", index: 0, input: "0hello1world2", groups: undefined]
regY.exec(str)
//["0", index: 0, input: "0hello1world2", groups: undefined 'regy. exec(STR)' //null: regy. exec(STR) //[groups: undefined 'regy. exec(STR)' //null: regy. exec(STR) //"0", index: 0, input: "0hello1world2", groups: undefined]
Copy the code
2. Usage
As you can see from the above example, the most basic way to use regular expressions to match strings is the exec method of the regular object:
This method returns an array whose first element is the text that matches the regular expression, followed by the text (if any) that matches several subexpressions of the regular expression. If the match fails, null is returned
The exec method returns an array with two attributes in addition to length:
- Index: Indicates the position of the first character of the matched text
- Input: Stores the retrieved string
In addition to the exec method, we can also use the match method of String objects, which is used in a similar way to exec
var str="0hello1"; str.match(/\d+/); / / /"0", index: 0, input: "0hello1", groups: undefined] str.match(/\d+/g); / / /"0"."1"]
Copy the code
Note the difference: when performing a global search, the match method returns an array of all matched characters with no index and input attributes. When no global search is performed, the two can be considered equivalent
In addition, regular expressions can test for compliance with a string using the test method, which returns a Boolean value
var reg=/hello\d+/;
reg.test("hello1") / /true
reg.test("hello JS") / /false
Copy the code
3. Advanced use method
Constrains the start and end characters of a string
There are many situations where we want to control the beginning or end of a string, such as not starting with a number or ending with a character.
- ^ represents the beginning of a line
- $indicates the end of the line
Assert that match
Further restrictions on character positions:
- Predicate: only x matches before y, write /x(? =y)/
- Prior negation assertion: matches only if x is not in front of y, write /x(? ! y)/
- After assertion: only x after y matches, write /(? <=y)x/
- Trailing negative assertion: matches only if x does not come after y, /(?
Group matching and named group matching
Ordinary group matching uses subexpression to extract different parts of a string into an array
Named group matching: Give each subexpression a name that can be used again in the regular expression.
var reg=/(? <word>[a-z]+)\_\k<word>/; reg.test("hello_js") / /true
Copy the code
In the above code, we first define a named subexpression word that matches lowercase letters, and then use the subexpression again. The entire regular expression matches two lowercase words that are joined by underscores
What can I do with regular expressions
0. Determine a specific string
The most convenient way to verify that a string conforms to a particular format is through regular expressions:
/ / it is in line with the variable naming rules, v is to judge the variable name var value = / [a zA - Z \ _ \ $] [0-9 a zA - Z \ _ \ $] * /. value.test(v)Copy the code
1. Cut the string
When reading a user input data in the data between the separator may be varied, and some people use a space, some people use more Spaces, some people with a comma pause period semicolons, and we read an entire string, how to put the string into individual data items, regular can play right now
Var reeg = / [\ s \ \ \. \] + / var data = dataInput. Split (reg);Copy the code
Writing this way, the program’s fault tolerance rate will be significantly improved
2. Extract the string information
As mentioned above, using the exec method or the match method of a String to get an array containing the contents of a String that matches the contents of a regular expression makes it easy to extract information from a String.
Note that re matching defaults to greedy matching, that is, matching as many characters as possible
var re = /^(\d+)(0*)$/;
re.exec('102300'); / / /'102300'.'102300'.' '] var reg=/^(\d+?) (0 *) $/; reg.exec('102300'); / / /'102300'.'1023'.'00']
Copy the code
3. Replace the string
The replace method of the string object can be used to replace a part of the string. Using the regular string, you can easily select a specific character in the string. For example, when displaying customer information, you need to replace the amount of money in the string with X
str.replace(/\d+/,"X");Copy the code