天天看點

第02章節-Python3.5-JS正則詳解11、JS 正則

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

  • 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]