正则表达式-基础实战篇
在理论基础篇之后呢,应该就对正则表达式有了一些了解.比如说如何去创建一个正则表达式以及其中的匹配规则等等.那么就开始正则表达式的实战吧! 建议把所有的实例在console窗口敲一遍.例子中展现的只是一部分,配合《正则表达式-理论基础篇》SF地址、原址使用效果更佳哦!
一个手机号码匹配的进化历程
实例代码依托于:RegExpObj.test(String) ,其含义是测试正则表达式与指定字符串是否匹配。成功匹配返回true
第一阶段-字符直接量:匹配自身
先假设一个手机号码为13762571094.
/13762571094/.test("13783281094");//false
/13762571094/.test("13762571094");//true
/13762571094/.test("ui13762571094dd");//true
//正则表达式在匹配时,只要输出匹配内容就返回true,不考虑前面的ui和后面的dd
//最后这种情况显然不是我们想要的.那么就进入下一阶段的实践吧.
第二阶段-锚点:指定匹配位置
/^http:/.test("http://www.163.com");//true
/^http:/.test("ahttp://www.163.com");//false
/^http:/.test("https://www.163.com");//false
/.jpg$/.test("1.jpg");//true
/.jpg$/.test("1.jpg png");//false
/.jpg$/.test("1.png");//false
/.jpg$/.test("regexp.png");//false
/bisb/.test("this");//false
/bisb/.test("that is reg");//true
/^13762571094$/.test("13762571094");//true
/^13762571094$/.test("ui13762571094dd");//false
//此时这个程序就能正确识别头尾的字符了.下面我们看看
/^13762571094$/.test("13712345674");//false
/*在试过了多个号码后发现,这个正则只能识别这个标准的手机号码.
这显然不是我们想要的,而不是识别一个手机号码的格式.
在下一阶段我们将实现一个手机号码的匹配.*/
第三阶段-字符类:匹配一类字符中的一个
[0-9]/.test("123")//true
/[0-9]/.test("asd")//false
/[^0-9]/.test("asd")//true
/[a-z]/.test("asd")//true
/./.test("allen")//true
/./.test("12")//true
/^1[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test("13762571094");//true
/^1[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test("13712345678");//true
//是不是感觉代码太长了呢?[0-9]足足5个字符呢,为了省力,当然不会就这样算了.继续向下看吧
元字符-具有特殊意义的字符
其实我们已经在前面使用过它啦
^ 、$ 、b 。
d :[0-9] 。D :[^d]
s :空白符。S :[^s]
w :[A-Za-z0-9_] 。W :[^w]
/d/.test("123");//true
/d/.test("1dsf");//true
/D/.test("1dsf");//true
/D/.test("123");//false
//自己再多去实验一些例子吧
/^1dddddddddd$/.test("13762571094");//true
/^1dddddddddd$/.test("13712345678");//true
/^1dddddddddd$/.test("1376257109x");//false
//是不是感觉代码比刚刚短了很多了呢?但这还是不够,什么事情都阻止不了我想偷懒的心,继续学习吧.
第四阶段-量词:出现的次数
{n,m} :n到m次。? :{0,1}
+ :{1,} 。* :{0,}
/d*/.test("abc");//true
/d+/.test("abc");//false
/d+/.test("1abc");//true
/^https?:/.test("http://www.163.com");//true
/^https?:/.test("https://www.163.com");//true
/^https?:/.test("httpss://www.163.com");//false
/^1d{10}$/.test("13762571094");//true
/^1d{10}$/.test("13712345678");//true
/^1d{10}$/.test("1376257109x");//false
//到这里手机号码就匹配完成了!
需要注意的地方-转义符:需要匹配的字符是元字符
/^http:/// ../@163.com$/
/http:///.test("http://www.163.com");//true
/@163.com$/.test("abc@163.com");//true
/@163.com$/.test("abc@163acom");//true
/@163.com$/.test("abc@163.com");//true
/@163.com$/.test("abc@163acom");//false
多选分支:网易邮箱的匹配
/thi(c|n)k/ ===thi[cn]k
.(png|jpg|jpeg|gif)$ :检测一个文件是不是图片文件.
/(w+)@(163|126|188).com$/.test("guo啊圣诞节了@163acom")//false
/(w+)@(163|126|188).com$/.test("guodong111@163acom")//false
/(w+)@(163|126|188).com$/.test("guodong111@163.com")//true
捕获
获取匹配的字符串:String.match(regexp)
var url = 'http://blog.163.com/album?id=1#comment';
var reg = /^(https?:)//([^/]+)(/[^?]*)?(?[^#]*)?(#.*)?$/;
// var reg = /^(https?:)//([^/]+)([^?]*)([^#]*)(.*)$///与上面的正则效果相同.;
var arr = url.match(reg);
//arr[0]为原字符串."http://blog.163.com/album?id=1#comment"
//对应括号所匹配的字符
var protocol= arr[1]//"http:"
var host= arr[2]//"blog.163.com"
var pathname= arr[3]//"/album"
var search= arr[4]//"?id=1"
var hash= arr[5]//"#comment"
替换一个子串:str.replace(regexp/substr,replacement)
var str = "the price of tomato is 5,the price of apple is 10."
str.replace(/(d+)/,"$1.00")
"the price of tomato is 5.00,the price of apple is 10."
//使用全局模式
str.replace(/(d+)/g,the price of apple is 10.00."
var html = '<label>网址:</label><input placeholder="以http://起始">';
html = html.replace(/[<>]/g,function(m0){
switch(m0){
case '<':
return '<';
case '>':
return '>';
}
});
console.log(html);//<label>网址:</label><input placeholder="以http://起始">
更强大的检索 - regexpObj.exec(str)
更详尽的结果:index
过程的状态:lastIndex
感觉基本用不到..就不说了...喜欢就推荐一下吧^_^
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|