JS 正则表达式 初探

//1、字面量创建正则表达式
let hd = 'adad'
console.log(/a/.test(hd));//测试hd字符串里面有没有 a

let a = 'c'
console.log(/a/.test(hd));//如果定义一个变量的话,这里的a 不是定义变量a 还依旧是 hd 字符串中 a

//若是想使用变量a 
console.log(eval(`/${a}/`).test(hd)); //eval()函数会将传入的字符串当作JS代码来执行



//2、使用对象创建正则表达式
let hd = 'dasdasdfdawedf'
let reg1 = new RegExp('d','g')//后面的g 是全局匹配的意思
console.log(reg1.test(hd));

//可以使用变量
let a = 'a'
let reg2 = new RegExp(a,'g')
console.log(reg2.test(hd));




//3、选择符,或 |
let hd = 'weweew'
console.log(/we|a/.test(hd));//表示字符串中有没有we或者a  |或字符


//4、转义符\
let price = 23.34
//\d表示数字0~9    + 表示多个
//.表示除换行外任何字符 或者 .普通点    若要匹配点元字符  记得转义一下
console.log(/\d+.\d+/.test(price));//true

let reg = new RegExp('\d+.\d+');
//与上面的结果不一样,是因为在字符串中  \d 就是字符串 d
console.log(reg.test(price));//false
//所以将reg中的 \d+.\d+  改成  \\d+\\.\\d+  结果就一样了


//5、字符边界约束
let hd = "3cdscefwe"
//^ 限制从开头起始来进行匹配  $限制从末尾开始匹配
console.log(/^\d$/.test(hd));//这个正则表示的意思是,起始和结束都必须是数字




//6、\d \s \w等元字符
//元字符是正则表达式的中的最小单位 
let hd = '张三:010-99999999,李四:020-88888888'
//.test()是正则表达式的方法,用于检测一个字符串是否匹配某个模式,匹配返回true,不匹配返回false
// .match()是字符串的方法,表示使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回
console.log(hd.match(/\d{3}-\d{7,8}/g));//\d{3} 表示匹配三位数字  \d{7,8}表示匹配7-8位数字 g表示全局范围内
//  /D  表示除了数字都匹配  
// \s表示匹配空白  \S表示除了空白
console.log(/\s/.test("\nhd"));
// \w  字母数字下划线   \W 除了字母数字下划线
let email = "#$@2222222222@qq.comefwe"
console.log(email.match(/^\w+@\w+\.\w+$/));



//7、i g  模式修正符
i :  匹配的时候不区分大小写
g :  全局匹配


//8、多行匹配修正符 m
let hd = `
    #1 js,200元 #
    #2 php,300元 #
    #9 houdunren.com # 后盾人
    #3 node.js,180元 #
`
console.log(hd.match(/^\s*#\d+\s+.+\s+#$/gm));


//9、汉字与字符属性
let hd = "houdunren2010.不断发布教程,加油!"
// \p{L} 表示匹配字符    \p{P} 表示匹配标点符号
console.log(hd.match(/\p{L}/gu));
console.log(hd.match(/\p{P}/gu));




//10、lastIndex属性的作用
let hd = 'houdunren'
let reg = /\w/g;
//exec()用于检索字符串中正则表达式的匹配,匹配成功会返回一个数组,其中存放匹配的结果,如果未找到匹配的,则返回值为null
//之所以会出现相同的代码输出不同的情况,原因在于 lastIndex 属性会记录上一次搜索的位置
console.log(reg.exec(hd));//h
console.log(reg.exec(hd));//o
while((res = reg.exec(hd))) {
    console.log(res);
}




//11、y 模式 y 要是下一个匹配不到会终止,从头开始
let hd = 'udunren'
let reg = /u/y;
console.log(reg.exec(hd));//u
console.log(reg.lastIndex);//1
console.log(reg.exec(hd));//null
console.log(reg.lastIndex);//0




12、原子表的基本使用 []
let hd = 'houdunren'
console.log(hd.match(/[ue]/g));//表示匹配单个字符 u 或者 e

区间匹配
let hd = "2010"
console.log(hd.match(/[0-9]+/g));//使用原子表,表示匹配0-9的多个数字
let hd = 'dewdwedf'
console.log(hd.match(/[a-z]+/g));//使用原子表,表示匹配a-z的多个字母

//排除匹配
let hd = 'jduiehdfiuew'
console.log(hd.match(/[^ue]/gi));//除了 u  和  e  都匹配到了

原子表字符不解析
let hd = '(efw).+'
console.log(hd.match(/[()]/gi));//原子组内的 () 为括号运算符,而不是原子组    . +符号  原理一样

//使用原子表匹配所有的内容
let hd = `
    houdhwu
    dqwed
`
console.log(hd.match(/[\s\S]+/)[0]);//  s代表忽略换行符   \s是空白   \S是除了空白



//13、原子组初步相识
let hd = `<h1>houduanren</h1>`
let reg = /<(h[1-6])>([\s\S]*)<\/\1>/i
console.log(hd.match(reg));

//邮箱验证原子组的使用
let mail = 'd23rd32r3f4r@qq.cem'
let reg = /^[\w-]+@([\w-]+\.)+(com|org|cc|cn|net)$/i;
console.log(reg.test(mail));

 ?: 表示不记录到原子组编号当中
 +  表示匹配一个或者多个
 *  表示匹配0个或者多个
{2} 表示匹配2个



有关于字符串的方法 
search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。如果没有找到任何匹配的子串,则返回 -1。
match() 是字符串的方法,表示使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回
matchAll() 返回一个包含所有匹配正则表达式的结果及分组捕获组的迭代器。




正则断言链接:
断言链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值