This is the 12th day of my participation in the August More Text Challenge.

Regular Expression (often abbreviated as regex, regexp, or RE) uses a single string to describe and match a series of string search patterns that conform to a syntactic rule.

Search mode can be used for text search and text replacement

What is a regular expression?

A regular expression is a search pattern formed by a sequence of characters.

When you search for data in text, you can use search patterns to describe what you are looking for.

A regular expression can be a simple character or a more complex pattern.

Regular expressions can be used for all text search and text replacement operations

// Regular expression body/modifier (optional)
// Where modifiers are optional

var patt = /kenguba/i
Copy the code

/kenguba/ I is a regular expression

Kenguba is a regular expression body (for retrieval)

I is a modifier (search is case insensitive)

Create a regular expression object

Create objects through constructors

var reg = new RegExp("Regular expression")

var reg = new RegExp(/\d{5}/)
var str = "My phone number is 10086."
console.log(reg.test(str))  //true
Copy the code

⚠️ Note: Normal character escape rules (preceded by a backslash \) are required when using constructors to create regular objects. For example, the following is equivalent

var re = new RegExp("\\w+"); var re = /\w+/;
Copy the code

Create objects literally

var reg = / Regular expression /

var reg = / / \ d {1, 5}
var flag = reg.test("Lucky number :888")
console.log(flag)  //true
Copy the code

Metacharacters, modifiers, and qualifiers

The modifier

Modifiers are used to perform case-sensitive and global matching:

The modifier describe
i Performs case-insensitive matching
g Perform global matches (find all matches instead of stopping after finding the first match)
m Perform multi-line matching

The square brackets

Square brackets are used to find characters in a range:

expression describe
[abc] Find any character between square brackets
[^abc] Find any character that is not between square brackets
[0-9] Find any number from 0 to 9
[a-z] Find any character written from small a to lowercase Z
[A-Z] Find any character from capital A to capital Z
[A-z] Find any character from uppercase A to lowercase Z
[adgk] Finds any character in a given collection
[^adgk] Finds any character outside the given collection
(red blue

metacharacters

A Metacharacter is acharacter that has a special meaning:

metacharacters describe
. Find single characters, except newlines and line terminators
\w Find numbers, letters, and underscores
\W Find non-word characters
\d Find the number
\D Find non-numeric characters
\s Finding whitespace characters
\S Find non-whitespace characters
\b Matching word boundaries
\B Matches non-word boundaries
\ 0 Finding NULL characters
\n Find a newline character
\f Find the feed character
\r Find carriage return
\t Find tabs
\v Find vertical tabs.
\xxx Find the character specified as the octal number XXX
\xdd Find characters specified in hexadecimal number dd
\uxxxx Finds Unicode characters specified in hexadecimal XXXX

quantifiers

quantifiers describe
n+ Matches any string that contains at least one n. For example, /a+/ matches the “A” in “candy”, all the “A” in “caaaaaaandy”
n* Matches any string containing zero or more n’s. For example, /bo*/ matches “Boooo” in “A Ghost Booooed “, “B” in “A bird Warbled “, but does not match “A goat grunted”
n? Matches any string containing zero or one n. For example, / e? le? / Match “el” in “angel”, “le” in “Angle”
n{X} Matches a string containing a sequence of X n. For example, /a{2}/ does not match the “A” in “candy,” but matches the two “A” in “caandy,” and the first two “A” in “caaandy.”
n{X,} X is a positive integer. The preceding pattern n matches if it occurs at least X times in a row. For example, /a{2,}/ does not match the “A” in “candy”, but matches all the “a” in “caandy” and “caaaaaaandy.”
n{X,Y} X and Y are positive integers. The preceding pattern n is matched at least X times and at most Y times. For example, /a{1,3}/ does not match “cndy”, matches “a” in “candy,” two “a” in “caandy,” and matches the first three “a” in “caaaaaaandy”. Note that when “caaaaaaandy” is matched, the match is “AAA” even though the original string has more “A”
n$ Matches any string ending in n
[^n](www.runoob.com/jsref/jsref…) Matches any string starting with n
? =n Matches any string immediately followed by the specified string n
? ! n Matches any string that is not immediately followed by the specified string n

RegExp objects and properties

RegExp object method

methods describe
compile Deprecated in version 1.5. Compiling regular expressions
exec Retrieves the value specified in the string. Returns the value found and determines its position
test Retrieves the value specified in the string. Returns true or false
toString Returns a string of regular expressions

String methods that support regular expressions

methods describe FF IE
search Retrieves the value that matches the regular expression 1 4
match Finds a match for one or more regular expressions 1 4
replace Replaces the substring that matches the regular expression 1 4
split Splits a string into an array of strings 1 4

RegExp object property

attribute describe
constructor Returns a function that is a prototype for creating a RegExp object
global Determines whether the “g” modifier is set
ignoreCase Determines whether the “I” modifier is set
lastIndex Specifies the starting position for the next match
multiline Determines whether the “m” modifier is set
source Returns the matching pattern of the regular expression
var str = China Telecom 10010, China Mobile 10086, China Unicom 10000
var matchList = str.match(/\d{5}/g)
for (var i = 0; i < matchList.length; i++) {
  console.log(matchList[i])
}
/* 10010 10086 10000 */
Copy the code