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如下图:
-
exec - 获取匹配的数据方法:
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]