Mainly record some of their easy to forget the regular characters and methods.
A, character
\D: digit, can also be used [0-9] \D: non-digit, can also be used [^0-9]
\W: letters, numbers, underscores, word characters, can also be [A-za-z0-9_], w is short for word \W: non-word character
\s: blank space, including space, horizontal TAB, vertical TAB, line feed, carriage return, page feed, or [\t\v\n\r\f], s is short for space \S: non-blank
.: wildcard characters, excluding line feeds, carriage returns, line separators, and segment separators
Match any character
[\d\ d] or [\w\ w] or [\s\ s] or [^]
Second, the definition of
Character groups
1.[0-9]: Any character in 0-9 can be used
2.[^0-9]: a non-numeric character. The first digit of the character group is an inverse sign to indicate the concept of inverse
quantifiers
? : equivalent to {0,1} +: equivalent to {1,} *: equivalent to {0,}
Meow character
^ : at the beginning? ! ^: not beginning $: end
"hello".replace(/^|$/g.The '#');
// #hello#
Copy the code
\ B: Word boundary, the position between \w and \w, the position between \w and ^, or the position between \w and $
"[JS] Lesson_01.mp4".replace(/\b/g.The '#');
// [#JS#] #Lesson_01#.#mp4#
Copy the code
(? =p): the character before p
"hello".replace(/ (? =l)/g.The '#'); // he#l#l0
Copy the code
(? ! p): (? =p) opposite meaning
"hello".replace(/ (? ! l)/g.The '#') // #h#ell#o#
Copy the code
Match the pattern
G: global
M: multiple lines
I: Case differences are ignored
Substitution operator
methods
If you need to know if a string matches a RegExp with a regular expression, use regexp.test (). If you just want to find the first match, you might need regexp.exec () instead. If you want to get and set a global flag capture group, you need to use the RegExp. The exec () or String. The prototype. The matchAll () instead.
The test method
str.match(reg)
Three models
1. If regexp does not carry the G flag, it returns the first match as an array containing the grouping and attribute index (the position of the match), input (the input string, equal to STR) :
'For more information, see Chapter 3.4.5.1'.match(/see (chapter \d+(\.\d)*)/i)
// ['see Chapter 3.4.5.1', // // JavaScript (full match)
// 'Chapter 3.4.5.1', // Script
// '.1', // Script (second grouping, last match)
// index: 22, // 7
// Input: 'For more information, see Chapter 3.4.5.1'
Copy the code
2. If regexp has a G flag, it returns an array of all matches as a string, without grouping and other details.
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.match(/[A-E]/gi)
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
Copy the code
"ababa abbb ababab".match(/(ab)+/g)
// ["abab", "ab", "ababab"]
Copy the code
3. If there is no match, null is returned with or without the tag G
str.matchAll(reg)
Search for all matches for all groups
There are three differences from Match:
It returns an iterable containing matches, not an array. We can use array. from to get a regular Array.
Each match is returned as an array containing groups (the return format is the same as str.match without the G tag).
If there is no result, instead of null, an empty iterable is returned.
Array.from('Hello, world!
'.matchAll(/ < (. *?) >/g))
Copy the code
str.search(reg)
Finds the location where the first re is matched
str.replace(reg, str|func)
The arguments in func are the contents of matching entries, and the order of arguments is STR and offset input
reg.exec(str)
reg.test(str)
Third, the commonly used
The thousands separator representation of a number
For example, “12345678” becomes “12,345,678”.
function toThousands(str) {
return str.replace(/ (? ! (^)? =(\d{3})+$)/.', ')}Copy the code
Refer to the article
Regular expression