在理论基础篇以后呢,应该就对正则表达式有了一些了解.好比说如何去建立一个正则表达式以及其中的匹配规则等等.那么就开始正则表达式的实战吧!
建议把全部的实例在console窗口敲一遍.例子中展示的只是一部分,配合《正则表达式-理论基础篇》SF地址、原址使用效果更佳哦!javascript
实例代码依托于:
RegExpObj.test(String)
,其含义是测试正则表达式与指定字符串是否匹配。成功匹配返回true
html
先假设一个手机号码为13762571094.java
最初形态git
/13762571094/.test("13783281094");//false /13762571094/.test("13762571094");//true /13762571094/.test("ui13762571094dd");//true //正则表达式在匹配时,只要输出匹配内容就返回true,不考虑前面的ui和后面的dd //最后这种状况显然不是咱们想要的.那么就进入下一阶段的实践吧.
^
匹配起始位置github
/^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
\b
:匹配单词边界segmentfault
/\bis\b/.test("this");//false /\bis\b/.test("that is reg");//true
在了解锚点以后咱们的正则就有了第一次进化api
/^13762571094$/.test("13762571094");//true /^13762571094$/.test("ui13762571094dd");//false //此时这个程序就能正确识别头尾的字符了.下面咱们看看 /^13762571094$/.test("13712345674");//false /*在试过了多个号码后发现,这个正则只能识别这个标准的手机号码. 这显然不是咱们想要的,而不是识别一个手机号码的格式. 在下一阶段咱们将实现一个手机号码的匹配.*/
[abc]
:a或b或c。[0-9]
:一个数字app
[^0-9]
:非数字的一个字符。[a-z]
:一个字母函数
.
:任一字符(换行符除外)
[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
了解了字符类后,咱们就能够进行第二次进化(50%)。此时就能匹配一个手机号码啦!
/^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 //本身再多去实验一些例子吧
了解了元字符后咱们就能够进行第二次进化(100%)了。
/^1\d\d\d\d\d\d\d\d\d\d$/.test("13762571094");//true /^1\d\d\d\d\d\d\d\d\d\d$/.test("13712345678");//true /^1\d\d\d\d\d\d\d\d\d\d$/.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
此时咱们的匹配手机号码的正则表达式就到了最后阶段了
/^1\d{10}$/.test("13762571094");//true /^1\d{10}$/.test("13712345678");//true /^1\d{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
保存匹配到的字符串,往后再用
()
:捕获/(\w+)@(163|126|188)\.com$/
(?:):不捕获/(\w+)@(?:163|126|188)\.com$/
使用:
$1,$2,...
api参数或返回值
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,"$1.00") "the price of tomato is 5.00, 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
感受基本用不到..就不说了...喜欢就推荐一下吧^_^