This is my first article on getting started
preface
In daily work, regular expressions are more or less exposed to either front-end development or back-end development. Regular expressions can be used to solve the problems we encounter in development and improve our work efficiency
Three uses of front-end regular expressions
- Parse the URL to get the parameters in it
- Form validation
- Sensitive word filtering
Basic methods in regularization
1.test()
- The result returned is a Boolean value
- If true, it means that the string conforms to the rule
- False indicates that the string does not conform to the rule
var reg = /a/
var str = "abc"
console.log(reg.test(str))
Copy the code
2.exec()
- Determines whether the target string contains characters that match the rule
- Returns an array, if any, containing the proper characters, the subscript, and the target string
- If not, return null
var reg = /a/
var str = "abc"
console.log(reg.exec(str))
Copy the code
Character classes
- [] is one digit. Empty [] indicates that any character can be matched
- If there is a character in [], [ABC] means that a, B, or C in this position is consistent with the rule (note: matching lowercase letters can be written as [a-z])
- The up arrow indicates no if written inside the character class, start if written outside the character class, and $indicates end
- [A-Za-Z0-9] indicates that the digit is either a lowercase letter or a number
The basic characters of the
The title | The title |
---|---|
[…]. | Any character in square brackets |
[^…]. | Any other character except the square brackets |
. | Any character other than the newline and other Unicode terminators |
\w | Any ASCII character word equivalent to [a-za-Z0-0] and containing an underscore |
\W | Any word that does not consist of ASCII characters, equivalent to [^ a-za-z0-9_] |
\s | Any Unicode whitespace |
\S | Any character that is not Unicode whitespace, note the difference between \w and \s |
\d | Any ASCII number, equivalent to [0-9] |
\D | Any character other than an ASCII number, equivalent to [^0-9] |
quantifiers
- {m} is repeated m times
- {n,m} means at least n and at most m occurrences
- {n,} indicates the minimum number of occurrences
- “+” is equivalent to {1,}
- “*” is equivalent to {0,}
- “?” Equivalent to {0, 1}
grouping
When () is used, it is equivalent to grouping the parentheses into a group. After grouping, the matching content can be retrieved from the group
The number following RegExp.&1 $indicates the number of groups
Parse the URL and obtain the name through the re
To fetch a group, you must run the re, e: reg.test(STR), before fetching.
If you want to write variables in a regular expression, declare the regular expression as an object
var reg = new RegExp(name+"= (/ ^ & *)")
reg.test(str)
// So we get the value after name. Name is the variable. This is the re that gets the value from the URL
Copy the code
Filter text content
var reg = / brother/g
var str = "I'm the big brother and you're the big brother. Hello, brother."
str.replace(reg,"*")
/ / if you want to match brother and elder brother two word available candidates, namely reg = / brother / | Columbia, | said the candidate, and [brother elder brother]
Copy the code
Note: g indicates global, I indicates case insensitive, and you can write it at the end
/\d{2,6}/ by default, the regular expression matches six digits. If you want to use lazy mode, add? That / \ d {2, 6}? /