Let’s try them out
// Verify the methodfunction verificationRule(reg,str){
return reg.test(str)
}
Copy the code
Examples of boundaries
VerificationRule (/\bjavascript/,'javascript') / /trueVerificationRule (/script\b/,'javascript') / /true// ^ matches the beginning of the line, and $matches the end of the line. VerificationRule (/^\wa$/,'mike_ma') / /true
Copy the code
Examples of times
// Match one or more Verificationrules (/\w+/,'hello') / /true// Match 0 to 1 verificationRule(/ Ed? /,'hello') / /true// Match 0 to multiple Verificationrules (/ Ed */,'mike') / /true
verificationRule(/ed*/,'word') / /true
verificationRule(/ed*/,'javascript') / /falseVerificationRule (/\d{3}/,'5654') / /true
Copy the code
It’s easy to ignore word boundaries and return true if the re matches
verificationRule(/ed*/,'vghfgygihedk') / /true
verificationRule(/^ed*$/,'vghfgygihedk') / /false
verificationRule(/^ed*$/,'ed') / /true
Copy the code
Examples of or
Said or meta characters commonly used two | and []; Each metacharacter in parentheses indicates that it may exist within the string.
verificationRule(/x|y/,'xffasihdg') / /true
verificationRule(/x|y/,'yasdngkia') / /true
verificationRule(/x|y/,'awett') / /false
verificationRule(/[x,y]/,'yasdngkia') / /true
verificationRule(/[x,y]/,'xjaskuifhx') / /true
verificationRule(/[x,y]/,'asfdas') / /false
Copy the code
Represents a grouping example
X and y above, if we want to match x or y exactly, we use grouping ().
verificationRule(/^(x|y)$/,'x') / /true
verificationRule(/^(x|y)$/,'y') / /true
verificationRule(/^(x|y)$/,'xy') / /false
Copy the code
Examples representing ranges
Brackets indicate possible characters. Numbers can be denoted as [0-9] in addition to \d. The same applies to upper case letters a-z as [a-z] and lower case letters as [a-z]. We can also use brackets to indicate the reverse, as in [^ ABC] except for ABC
/^[0-9a-zA-z_]+$/ equivalent to /^\w+$/ /^[^\s]+$/ equivalent to /^\ s +$/Copy the code
An example of a modifier
Modifiers are relatively simple, with only three
// g global match"this is test".match(/is/g); / / /"is"."is"] // m multi-line matching"\nIs th\nis it?".match(/^is/m); / / /"is"] // I ignores case"Hello Word".match(/word/i) // ["word"] ` `Copy the code