为了账号安全,请及时绑定邮箱和手机立即绑定

正则表达式[JavaScript之美]

标签:
JavaScript
1、创建正则表达式对象
  • 方式一: new RegExp(pattern, attributes);
  • 方式二: /pattern/attributes (一般用此方式)

pattern : 模式字符串
attributes : 可选字符串, 可以为:

  • i : 忽略大小写 (ignoreCase)
  • g : 全局匹配 (global)
  • m : 多行匹配 (multiline)
2、调用此方法
  • regexp.test(string)
    如果字符串string中含有与regexp匹配的文本,就返回true,否则返回false。
  • regexp.exec(string)
    从字符串string中找出regexp匹配的字符串,返回字符串组成的数组。如果没有找到匹配,返回null。
3、正则表达式语法
  • 元字符:
    W3school正则表达式-元字符
console.log(/./.exec("imooc  2016"));//查找单个字符
console.log(/\w/.exec("imooc  2016"));// world, 查找单词字符
console.log(/\d/.exec("imooc  2016"));// digits, 查找数字
console.log(/\s/.exec("imooc  2016"));// space, 查找空白字符
  • 方括号 :
    W3school正则表达式-方括号
console.log(/[mooc]/.exec("iiiiiimooc"));// 查找方括号之间的任何字符
console.log(/[^mooc]/.exec("imooc2016imooc"));// 查找任何不在方括号之间的字符。
console.log(/[0-9]/.exec("imooc2016imooc"));// 查找任何从 0 至 9 的数字。
console.log(/[a-z]/.exec("imooc2016imooc"));// 查找任何从小写 a 到小写 z 的字符。
console.log(/[A-z]/.exec("imooc2016imooc"));// 查找任何从大写 A 到小写 z 的字符。
  • 量词 :
    W3school正则表达式-量词
console.log(/o+/.exec("iiiiiimooc")); //结果为: oo,  匹配任何包含至少一个 i 的字符串。 尽可能多的去匹配
console.log(/i*/.exec("iiiiiimooc")); //结果为: iiiiii,  匹配任何包含零个或多个 i 的字符串。尽可能多的去匹配
console.log(/i?/.exec("iiiiiimooc")); //结果为: i,  匹配任何包含零个或一个 i 的字符串。
console.log(/i{2}/.exec("iiiiiimooc")); //结果为: ii,  匹配包含 2 个 i 的序列的字符串。
console.log(/i{2,4}/.exec("iiiiiimooc")); //结果为: iiii,  匹配包含 2 到 4 个 i 的序列的字符串。 尽可能多的去匹配
console.log(/i{2,}/.exec("iiiiiimooc")); //结果为: iiiiii,  匹配包含至少 2 个 i 的序列的字符串。 尽可能多的去匹配
console.log(/^i/.exec("iiiiiimooc")); //结果为: i,  匹配任何开头为 i 的字符串。
console.log(/i$/.exec("iiiiiimooc")); //结果为: null(没有匹配到),   匹配任何结尾为 i 的字符串。
4、lastIndex属性

一个整数,标示开始下一次匹配的字符位置。
(W3school)说明:

  1. 该属性存放一个整数,它声明的是上一次匹配文本之后的第一个字符的位置。
  2. 上次匹配的结果是由方法 RegExp.exec() 和 RegExp.test() 找到的,它们都以 lastIndex 属性所指的位置作为下次检索的起始点。这样,就可以通过反复调用这两个方法来遍历一个字符串中的所有匹配文本。
  3. 该属性是可读可写的。只要目标字符串的下一次搜索开始,就可以对它进行设置。当方法 exec() 或 test() 再也找不到可以匹配的文本时,它们会自动把 lastIndex 属性重置为 0。
var reg = /aba/ig;
console.log(reg.exec("abaABA"));// lastIndex为0
console.log(reg.test("abaABA"));// lastIndex为3  
console.log(reg.exec("abaABA"));// lastIndex为6
console.log(reg.exec("abaABA"));// lastIndex为0, 没有找到可以匹配的文本, 此时lastIndex为0, 即重新查找

//W3school示例:               
var str = "The rain in Spain stays mainly in the plain";
var patt1 = new RegExp("ain", "g");

patt1.test(str)
document.write("找到匹配,索引目前在: " + patt1.lastIndex + "<br />");
patt1.test(str)
document.write("找到匹配,索引目前在: " + patt1.lastIndex + "<br />");
patt1.test(str)
document.write("找到匹配,索引目前在: " + patt1.lastIndex + "<br />");
patt1.test(str)
document.write("找到匹配,索引目前在: " + patt1.lastIndex + "<br />");
patt1.test(str)
document.write("找到匹配,索引目前在: " + patt1.lastIndex + "<br />");

不得不说:

不具有标志 g 和不表示全局模式的 RegExp 对象不能使用 lastIndex 属性。

5、子正则
console.log(/(^\w{5,})\.([a-z]{3})/.exec("imooc.com"));// "imooc.com, imooc"
//注意 (^\w{5,}) , ([a-z]{3}$) 是加了()的, 它们也会作为一个正则表达式(子正则), 匹配目标字符串, 匹配到了就一块返回, 我们会发现它们总会返回。
6、 String中有关的正则方法

W3schoolString-正则方法

  • string.search(reg) 检索与正则表达式相匹配的子串, 找到返回其下标,否则返回-1 功能类似于reg.test(str)
  • string.match(reg) 返回匹配的第一个或所有子串组成的数组
  • string.replace(reg, newString) 将匹配的子串替换为新的字符串
  • string.split(reg) 用匹配的子串将字符串分隔成数组
//示例1 : 判断字符串中是否包含标签字符(html/body)
console.log("imoochtmlimooc".search(/html|body/) != -1); // true 有

//示例2 : 得到字符串中所有的"love"及其后面的一个字符
console.log("I Love Imooc, do you love?".match(/love./ig));

//示例3 : 得到多行文本中每行结尾的2个字符
console.log("imooc\ntanya\nalin".match(/\w{2}$/mg));//mg 多行匹配和全局匹配

//示例4 : 将字符串中所有的标签字符(html/body)替换为***
console.log("imoochtmlimoocbodyimooc".replace(/html|body/ig, "***"));

//示例5 : 将"www-imooc+com"分隔成['www', 'imooc', 'com']
console.log("www-imooc+com".split(/[-+]/ig));
点击查看更多内容
88人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消