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

阿啦卜發表於2018-07-20

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 = /Javaw*/g;)正則後加g

text="JavaScript is more fun than Java or JavaBeans!"
"JavaScript is more fun than Java or JavaBeans!"
var pattern = /Javaw*/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


相關文章