正则-看这篇就足够了

2023-11-21  本文已影响0人  小仙有毒_1991

前端业务中经常会遇到一些表单输入项的校验场景,网上百度一大堆,质量良莠不齐。
为了保证系统的安全可靠,掌握基本的正则使用和阅读能力已经迫在眉睫
推荐一款生产力工具 正则表达式
通过上述工具生成如下流程图,更好的理解复杂正则

正则分析图.png

开始之前先阅读以下需求,如果答案能够脱口而出,那么恭喜你已经毕业,不用往下看了~

//正则表达式的方法
let regx = /^a&/
regx.exec(string) //一个在字符串中执行查找匹配的 RegExp 方法,它返回一个数组(未匹配到则返回 null)。
regx.test(string) //一个在字符串中测试是否匹配的 RegExp 方法,它返回 true 或 false。
String的
"apple".match(regx)//一个在字符串中执行查找匹配的 String 方法,它返回一个数组,在未匹配到时会返回 null。
"apple".replace(regx,"") //一个在字符串中执行查找匹配的 String 方法,并且使用替换字符串替换掉匹配到的子字符串,返回一个新的字符串。
通过flags执行高级搜索
let regx = /pattern/flags;
let regx = new RegExp("pattern", "flags");
g 全局搜索
i 不区分大小写
^ 匹配输入的开始。
let regx = /^A/
"an Abc".match(regx)    //null
"An Abc".match(regx)    //[A]
$ 匹配输入的结束。
let regx = /t$/ 
"eat".match(regx)    //[t]
"eater".match(regx)    //null
* 等价于 {0,}
限定符,匹配前一个表达式 0 次或多次。
let regx = /bo*/
"A ghost boooooed".match(regx)    //[booooo]
"A bird warbled".match(regx)    //[b]
+ 等价于 {1,}。
限定符,匹配前面一个表达式 1 次或者多次。
let regx = /a+/
"candy".match(regx)    //[a]
"caaaaaaandy".match(regx)    //[aaaaaaa]
? 等价于 {0,1}。
限定符,匹配前面一个表达式 0 次或者 1 次。
let regx = /e?le?/
"angle".match(regx)    //[le]
"oslo".match(regx)    //[l]
.
默认匹配除换行符之外的任意字符
let regx = /.n/
"Anthony".match(regx)    //[on]
let regx = /<.*>/    //贪婪匹配
"<image src='https://www.vchangyi.com'/>".match(regx)    //[<image src='https://www.vchangyi.com'/>]
let regx = /<.*?>/    //非贪婪或最小匹配
"<h1>您好</h1>".match(regx)    //[<h1>]
|
指明两项之间的一个选择
let regx = /a|b/
"abc".match(regx)    //[a]
(x)
它会匹配 'x' 并且缓存匹配项。其中括号被称为捕获括号
let regx = /(foo) (bar)/
"foo bar foo bar".match(regx) //   ['foo bar', 'foo', 'bar'] 数组的第一项是进行匹配完整的字符串,之后的项是用圆括号捕获的结果。
let regx = /(foo) (bar) \1 \2/
"foo bar foo bar".match(regx) //  ['foo bar foo bar', 'foo', 'bar']  数组的第一项是进行匹配完整的字符串,之后的项是用圆括号捕获的结果。
let regx = /\b([a-z]+) \1\b/ig
"Is is the cost of of gasoline going up up".match(regx) // ['Is is', 'of of', 'up up']
(?:x)
匹配 'x' ,消除缓存,这种括号叫作非捕获括号,使得你能够定义与正则表达式运算符一起使用的子表达式。
let regx = /(?:foo){1,2}/
"foofoo".match(regx) //[foofoo]
"fooo".match(regx) //[foo]
let regx = /foo{1,2}/
"foofoo".match(regx) //[foo]
"fooo".match(regx) //[fooo]
x(?=y)先行断言(正向肯定预查)
仅仅当'x'后面跟着'y',匹配'x'
let regx = /Java(?=script)/
"Java".match(regx) //null
"Javascript".match(regx) //[Java]
let regx = /Java(?=script|Spring)/
"Javascript".match(regx) //[Java]
"JavaSpring".match(regx) //[Java]
(?<=x)Y 后行断言(反向肯定预查)
仅仅当'x'后面跟着'y',匹配'y'
let regx = /(?=type|java)script/ //1 错误示范
"javascript".match(regx)// null
"typescript".match(regx)// null
let regx = /(?<=type|java)script/ //2 正确示范
"javascript".match(regx)//[script]
"typescript".match(regx)//[script]
x(?!y) 先行否定断言(正向否定查找)
仅仅当'x'后面不跟着'y'时,匹配'x'
let regx = /type(?!script)/
"typescript".match(regx)    //null
"typeof".match(regx)    //[type]
(?<!y)x 后行否定断言(反向否定查找)
仅仅当'x'前面不是'y'时,匹配'x'
let regx = /(?<!type|java)script/ 
"javascript".match(regx)//null
"typescript".match(regx)//null
"script".match(regx)//[script]
{n}
n 是一个正整数,匹配了前面一个字符 "刚好"出现了 n 次
let regx = /p{2}/
"apple".match(regx)//[pp]
"people".match(regx)//null
"pppp".match(regx)//[pp]
{n,}
n 是一个正整数,匹配前一个字符"至少"出现了 n 次。
{n,m}
n 和 m 都是整数。其中 n <= m,匹配前面的字符至少 n 次,最多 m 次。如果 n 或者 m 的值是 0,这个值被忽略。
[abcd]  等价于 [a-d]
一个字符集合。匹配方括号中的任意字符
let regx = /[a-z.]/
"test.i.ng".match(regx)    //[t]
[^abc]  等价于 [^a-c]
let regx = /[^a-c]/
一个反向字符集。不匹配方括号中的任意字符
"apple".match(regx)   //[p]
\d 等价于 [0-9] 匹配一个数字
\D 等价于 [^0-9] 匹配一个非数字字符
\w 等价于 [A-Za-z0-9_]
\W 等价于 [^A-Za-z0-9_]
\
在非特殊字符之前的反斜杠表示下一个字符是特殊字符  \d \D \w  \W
在特殊字符之前的反斜杠表示下一个字符不是特殊字符,代表转义  \\d  => d
/[a-z]\s/i 和 new RegExp("[a-z]\\s", "i") 创建了相同的正则表达式
例如 /[a-z]:\\/i 和 new RegExp("[a-z]:\\\\","i") 会创建相同的表达式
/[.*+\/?^${}()|[\]\\]/
//上述有几个转义  3
上一篇下一篇

猜你喜欢

热点阅读