Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.
1. Regexp Dotall Flag
Regular expression dotAll mode
1.1 introduction
In regular expressions, the dot. Is used to match any single character, but it cannot match four-byte UTF-16 characters (this problem can be solved by the U modifier), and line terminators.
Line terminators represent the end of a line, such as U+000A newline \n, U+000D carriage return \r, U+2028 line separator, U+2029 paragraph separator.
/name.alice/.test('name\nalice') // false
Copy the code
In the above code, point. Does not match the \n newline character, so return false. The s modifier has been introduced in ES2018 to allow. To match any single character. This pattern is called the dotAll pattern;
/name.alice/s.test('name\nalice') // true
Copy the code
2. Regexp Named Groups
Regular expression named capture group
2.1 introduction
Before ES2018, regular expressions can be matched by ()
const Reg_Day = /(\d{4})-(\d{2})-(\d{2})/
const matchList = Reg_Day.exec('2021-05-27')
const year = matchList[1] / / '2021'
const month = matchList[2] / / '05'
const day = matchList[3] / / '27'
Copy the code
One drawback of group matching is that it is not clear what each group of matches means, and the value can only be fixed by index.
After ES2018, named capture groups were introduced, allowing a name to be specified for each group match.
let users = `name: alice, age: 19
name: sam, age: 23`
let regexpNames = /name: (?
.+), age: (?
.+)/gm
let match = regexpNames.exec(users)
do {
console.log(`Hello ${match.groups.name} ${match.groups.age}`)}while((match = regexpNames.exec(users)) ! = =null)
// Hello alice 19
// Hello sam 23
Copy the code
3. Regexp Unicode Prope
Regular expression Unicode escape
3.1 introduction
Before ES2018, we used \d to match any number, \s to match any character that was not a space.
After ES2018, \p{} matches all Unicode characters. Its negation is \p{}.
/^\p{ASCII_Hex_Digit}$/u.test('0123456789ABCDEF') // true
/^\p{ASCII_Hex_Digit}$/u.test('abc') // false
Copy the code
The ASCII_Hex_Digit of the code above is a Boolean property that checks if the string contains only valid hexadecimal digits
There are many others like Uppercase, Lowercase, White_Space, Alphabetic, and Emoji
/^\p{Lowercase}$/u.test('h') // true
/^\p{Uppercase}$/u.test('H') // true
/^\p{Emoji}+$/u.test('H') // false
Copy the code
Praise support, hand left lingering fragrance, with rongyan, move your rich little hands yo, thank you big guy can leave your footprints.
Past wonderful recommendation
ES Series summary (3)
ES Series summary (2)
ES Series summary (1)
Front-end common encryption methods
Canvas Pit Climbing Road
Don’t know SEO optimization? An article helps you understand how to do SEO optimization
Canvas Pit Road
Wechat small program development guide and optimization practice
Talk about mobile adaptation
Front-end performance optimization for actual combat
Talk about annoying regular expressions
Obtain file BLOB stream address to achieve the download function
Vue virtual DOM confused? This article will get you through the virtual DOM once and for all
Git Git
Easy to understand Git introduction
Git implements automatic push
Interview Recommendations
Front ten thousand literal classics – the foundation
Front swastika area – advanced section
More exciting details: personal homepage