第02章节-Python3.5-JS正则详解1

1、JS 正则

(https://www.cnblogs.com/moqing/archive/2016/07/13/5665126.html)
(https://www.cnblogs.com/wupeiqi/archive/2016/07/13/5602773.html)

  • test - 判断字符串是否符合规定的正则
  • 正则表达式写法:
rep = /\d+/;
    
/\d+/
rep.test("dsfdsjkh1243824fdsfdskjg")
true
rep.test("dsfdsjkhfdsfdskjg")
false

在浏览器打开调试工具F12如下图:


p3a1.png

p1.png
  • exec - 获取匹配的数据方法:


    image.png

    image.png
  • JavaScript全局匹配:(var pattern = /\bJava\w*\b/g;)正则后加g

text="JavaScript is more fun than Java or JavaBeans!"
"JavaScript is more fun than Java or JavaBeans!"
var pattern = /\bJava\w*\b/g;
undefined
pattern.exec(text)
["JavaScript", index: 0, input: "JavaScript is more fun than Java or JavaBeans!", groups: undefined]
pattern.exec(text)
["Java", index: 28, input: "JavaScript is more fun than Java or JavaBeans!", groups: undefined]
pattern.exec(text)
["JavaBeans", index: 36, input: "JavaScript is more fun than Java or JavaBeans!", groups: undefined]
image.png

image.png
上一篇:第01 02章节-Python3.5-今日内容概要 1


下一篇:第07章节-Python3.5-Django基于正则表达式的URL(一) 6