preface
With the passage of time, the successful writing of several years of business, finally turned back to fill the gaps. In fact, no matter what development or language you are doing, you will use re. In retrospect, SOME of the regulars used in my daily business will be directly searched for and used. There are also some careful students will record down, list a document from time to time to update, use the time to directly find. In a word, what can be copied is not something to think about (perhaps this is the root cause of the dish).
Recently, while studying vue-Router, I saw that it matches two re segments of the path. To be honest, I don’t really get it. Plus, in today’s world interviewers are asking questions like, ‘Can I implement a xx regular? ‘, tired of being asked. Can’t I learn…
Re, hello
A regular expression is a text pattern. It includesNormal character
(‘1-9,a-z… ‘These) andSpecial characters
(‘ metacharacter ‘). We usually use itA string that matches a syntactic rule
It’s a seemingly cumbersome string, but it’s undeniably powerful (you get the idea?). Reason to work efficiency will have a qualitative improvement (in order to judge the phone number, not regular, judge the code has to be written to the monkey year).
role
- It can be used to replace a sentence or a piece of text or a string (globally replacing some copywriting).
- It can be used to verify that the format of a specified string complies with the rules (mobile phone number, email address, etc.).
- It can look for text specific fields.
Every now and then, when browsing the code of the boss, you will see a re. .Possible use? And *, or $\ / ^… what? Especially if it’s a long re, you can’t watch it at all.
Regular grammar
Normal character
Ordinary characters include all upper and lower case letters, all numbers, all punctuation marks and some other symbols.
- [abcde] matches all characters present in [] in a single string. .
var str = 'hello, bro'
var reg = /[abcde]/g
console.log(str.match(reg)) // ['e', 'b']
Copy the code
- [^abcde] matches all characters not present in [] in a string.
var str = 'hello, bro'
var reg = /[^abcde]/g
console.log(str.match(reg)) // ["h", "l", "l", "o", ",", " ", "r", "o"]
Copy the code
- [a-z] /[a-z] matches all uppercase/lowercase letters in A string.
var str = 'Hello, Bro'
var reg = /[A-Z]/g
console.log(str.match(reg)) // ["H", "B"]
Copy the code
- . Matches characters in a string except newline characters (\n, \r).
var str = 'hello, bro'
var reg = /.]/g
console.log(str.match(reg)) // ["H", "e", "l", "l", "o", ",", " ", "B", "r", "o"]
Copy the code
- [\s\ s] \s matches all whitespace characters in a string, \s matches all non-whitespace characters in a string.
var str = 'hello, bro'
var reg = /[\s]/g
console.log(str.match(reg)) // [" "]
Copy the code
- [\w] Matches letters, numbers, and underscores in a string.
var str = 'hello, bro_'
var reg = /\w/g
console.log(str.match(reg)) // ["H", "e", "l", "l", "o", "B", "r", "o", "_"]
Copy the code
Special characters
Special characters are characters that have special meanings (like… Let’s just say that some characters we use to generate regular expressions appear in a string and then need to match them out, so we need to escape \ with a backslash. Those that need to be escaped are called special characters.
- ‘$’ matches the end of the input string.
- ‘()’ represents a piece of content to be matched, marking the starting position.
- The ‘*’ matches the preceding expression zero or more times
- The ‘+’ matches the previous expression one or more times
- ‘.’ represents any single character other than a newline character
- ‘[]’ represents a bracketed expression
- ‘? ‘matches the preceding expression zero or once
- \ Escapes the next character mark or special character
- The ‘^’ matches at the beginning of the string, in parenthesis expressions to indicate that characters in parentheses do not match
- The ‘{}’ flag qualifier expression
- ‘|’ both choose a ‘or’ mean
qualifiers
Qualifiers are used to specify how many times a given expression of a regular expression must occur
- The ‘*’ same as above
- ‘+’ same as above
- ‘? The same goes for ‘
- ‘{n}’ n represents a non-negative integer that matches a certain n times
- ‘{n,}’ n represents a non-negative integer, with a comma indicating at least n matches
- ‘{n, m}’ m,n are non-negative integers indicating at least n matches and at most m matches
/ [0-9] {1, 2} / / / only 1-2 digits / [1-9] [0-9] {0, 1} / match 1-99 numberCopy the code
Non-print character
Some non-print characters on a page can also be part of a regular expression.
- ‘\n’ matches a newline character
- ‘\r’ matches a carriage return
- ‘\f’ matches a feed character
- ‘\s’ matches any whitespace characters including non-print characters above
[\f\n\r\t\v]
- ‘\S’ matches any non-whitespace character
Modified character
- ‘g’ global match, find all matching characters of the match
- ‘I’ is case insensitive
- ‘m’ multi-line match
- ‘s’ makes dot. Match \n
Greedy expression
- The + and + qualifiers are greedy because they match as much text as possible.
var str = '<div>Hello, Bro_</div>'
var reg = /<.*>/
console.log(str.match(reg)) // <div>Hello, Bro_</div>
Copy the code
Non-greedy expression
As opposed to greed, put a word after greed, right? Because? Matches zero or one time.
var str = '<div>Hello, Bro_</div>' var reg = /<.*? >/ console.log(str.match(reg)) // <div>Copy the code
conclusion
All of the above are called metacharacters except for ordinary characters, and some are not. But for now, this is enough for ordinary situations. It’s all about practice, otherwise it’s all empty talk. The examples in this article are written in JavaScript, which of course does not limit the language. Once you understand the basic syntax of regees, as well as the components, you need to practice them in your daily development.
Come on.