~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Development tools and key technology: Adobe Dreamweaver & amp; & JavaScript
Author: WeiYongGui
Writing time: April 26, 2020,
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Introduction of a,
Regular expressions: also known as the Regular Expression, English named Regular Expression, often abbreviated as regex in the code, the RegExp or RE
Regular expressions are used to define some rules of the string, a computer can according to the regular expression, to check whether a string is conform to the rules, can be extracted content conform to the rules of character [grammar: var variable=new RegExp (" regular expression ", "match the pattern");] Use the literal to create a regular expression [grammar: var variable=/regular expression matching mode]
Use the typeof check regular object, will return to the object
Varreg=new RegExp (" a "); This expression can be used to check whether a string containing the characters in a
In the constructor can pass a matching model as the second parameter matching] [I ignore case, g global
Regular expression [methods: test ()] - use this method can be used to check whether a string in accordance with the rules of the regular expression,
If conform to return true, otherwise it returns false
Second, the regular expression
[] to use the brace said range
[ABC] is equivalent to a | b | c
[a-z] means to match all lowercase
[a-z] means to match all the capital letters
[a-z] means to match all of the letters (with case)
[0-9] means to match all digital
In addition to all characters within square brackets [^]
^ said the opening
$said the ending
Three, the method of string associated with a regular expression
- support regular expression String object method
Methods: the split ()]
- the string to an array of strings,
- this method can pass a regular expression as a parameter, this method will be based on regular expressions to split
- this method even if they don't specify a global match, will all split string
Methods: the search ()]
- whether can search string containing the specified content
- if the search to specify the content, will appear for the first time return index, if there is no search to return 1
- it can take a regular expression as a parameter, then according to the regular expression to search string
- serach () will only find the first, even if it was useless to set the global matching
Methods: the match ()]
- can be according to the regular expression, from a string will be extracted from eligible content
- by default we match will only find the first meets the requirements of content, find later stopped searching
We can set a regular expression to match global mode, so that it will match to the contents of all
Can set multiple matches for a regular expression pattern, and order, there is no
- the match () will match to the contents of the package to the returned in an array, even if only one result query to
Methods: the replace ()]
- strings can be specified in the replace with new content
Is replaced - parameter description: 1. 2. The content of the new content
Four, the regular expression syntax (quantifier)
- through the quantifiers can set the number of occurrences of a content
- measure words in front of it only a content work
- {n} appear n
- {m, n} m times into n
- {n,} appear more than n
- appear at least once a +
- * a zero or zero or more times a
-? Once or zero
Five, the regular expression syntax (metacharacters)
\ backslash in the regular expression, as the escape character
To match the characters ". "regular expressions for \.
\ \ means to match a backslash \
The means to match any character
\ w match any letters, Numbers, "_" underline e/A - z0-9 _
\ W matching in addition to letters, Numbers, "_" underline e [^ A - z0-9 _]
\ d match Numbers [0-9]
\ D in addition to the number of [^ 0-9] any character
Matching \ s Spaces
\ S match any character except Spaces
\ b match word boundaries
\ B match the word boundaries,