正則是獨立于編程語言的一個學科,用于解決模式匹配問題,Javascript提供了對于正則支持,此外,Java、c、python也都支持正則,正則可以應用在:檢索,替換,爬蟲,論文查重等領域,
實體化正則運算式物件
- 字面量
var pattern = /正則運算式/標記
var pattern = /abc/igm - 建構式
var pattern = new RegExp(“正則運算式”,“標記”);
var pattern = new RegExp(“abc”,“igm”);
標記:
i ignoreCase 忽略大小寫
g global 全域
m multiline 多行
u unicode 任何 Unicode 代碼點的轉義都會被解釋,
y sticky 屬性反映了搜索是否具有粘性
正則運算式
-
直接量
abc 例如:/abc/ 查找目標串中是否含有abc -
字符類
[abc] 例如:/[abc]/ 查找目標串中是否含有abc中任意一個字符
[^abc] 例如:/[^abc]/ 查找目標串中是否含有除了abc之外任意一個字符
[a-z] a~z中的任意一個字符
\w 字母 [a-zA-Z0-9]
\W 非字母 [^a-zA-Z0-9]
\d 數字 [0-9]
\D 非數字[^0-9]
\s 空白符
\S 非空白符多行模式下
^ 以…開始 /^\d\w{3}\d$/ 以為數字開頭,以數字結尾
$ 以…結尾 -
數量詞
數量一般使用在子運算式(直接量,字符類,分組…)后
/1[3578]\d{9}/
{9} 重復9次
{1,9} 重復1~9次
{1,} 重復1次及以上
{0,} 重復0次及以上
* 等價于{0,}
+ 等價于{1,}
? 等價于{0,1}貪婪匹配
默認是貪婪匹配
{1,9} 優先匹配9次
非貪婪匹配
數量詞后添加?就成為了非貪婪匹配
{1,9}? 優先匹配1次 -
選擇
子運算式中間添加"|"表示選擇
例如:
/hello|hi/ -
分組
獲取目標字串中所有的url,并且分別拿到協議,ip,port,路徑
url 協議://ip:port/路徑
var pattern =
/(http|https|ftp|svn) \ : //((\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})|(www.\w{2,}.com)):?(\d{2,5})(/[a-z0-9/]{2,})/ig
/() : //(()|()): ()()/
- 參考
通過"\數字"對之前分組匹配結果的一種參考,\1 就表示對第一個分組匹配結果的參考
var str = “12hello12”
var str = “871wrold871”
var str = “871wrold888”var pattern = /(\d{2,})\w+\1/
API
1.實體屬性
RegExp.prototype.flags 標記
RegExp.prototype.source 正則字串
RegExp.prototype.ignoreCase
RegExp.prototype.global
RegExp.prototype.multiline
RegExp.prototype.unicode
RegExp.prototype.sticky
這里有個簡單的例子來直接的理解這些屬性的作用:
var pattern = /hello/igm;
console.log(pattern);// /hello/gim
console.log("source:",pattern.source);// source: hello
console.log("flags:",pattern.flags);// flags: gim
console.log("ignoreCase:",pattern.ignoreCase);// ignoreCase: true
console.log("global:",pattern.global);// global: true
console.log("multiline:",pattern.multiline);// multiline: true
console.log("unicode:",pattern.unicode);// unicode: false
2.實體方法
RegExp.prototype.test(str)
目標字串中是否可以滿足正則運算式的匹配要求
支持全域匹配,當全域匹配的時候,會在正則運算式物件pattern上維 護一個變數 lastIndex,表示下次開始檢索的位置,
引數:字串
回傳值:boolean
RegExp.prototype.exec(str)
從目標字串中獲取滿足正則運算式匹配要求的子串,
支持全域匹配,當全域匹配的時候,會在正則運算式物件pattern上維護一個變數,lastIndex,表示下次開始檢索的位置,
引數:字串
回傳值:陣列
陣列元素為匹配的結果
exec的回傳值的陣列中的第一個元素整體匹配的結果, 第二個元素為第一個分組結果, 第三個元素為第二個分組的結果
陣列屬性index表示當前子串在目標串中的位置,input表示目標串,groups表示分組
以下是一個檢索網址的例子:
//檢索網址
var str = "hello , i am terry, my website is http://134.175.154.93:8888/personal/index.html, ftp url is ftp://172.16.0.2
0/webui , my phone 18812345432, my friend is larry ,his telephone is 13710009999,his web site is http://www.larry.com/p
ersonal/my.index.html , his email is larry@briup. com, my other first is jacky, his telephoen is 17751229900";
//網址的正則運算式
var a = /(http|https|ftp|svn)\:\/\/((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(www\.\w{2,}\.com))\:?(\d{2,5})?(\/[a-z0-9/]{2,
})/ig
let result = null;
while(result = a.exec(str)){
console.log(result);
}
輸出的結果:
[
'http://134.175.154.93:8888/personal/index',//整體的匹配結果
'http',//第一個分組結果
'134.175.154.93',
'134.175.154.93',
undefined,
'8888',
'/personal/index',
index: 34, //當前子串在目標串中的位置
input: 'hello , i am terry, my website is http://134.175.154.93:8888/personal/index.html, ftp url is ftp://172.16.0.20/webui , my phone 18812345432, my friend is larry ,his telephone is 13710009999,his web site is http://www.larry.com/personal/my.index.html , his email is larry@briup. com, my other first is jacky, his telephoen is 17751229900', //檢索的目標串
groups: undefined//分組
]
[
'ftp://172.16.0.20/webui',
'ftp',
'172.16.0.20',
'172.16.0.20',
undefined,
undefined,
'/webui',
index: 93,
input: 'hello , i am terry, my website is http://134.175.154.93:8888/personal/index.html, ftp url is ftp://172.16.0.20/webui , my phone 18812345432, my friend is larry ,his telephone is 13710009999,his web site is http://www.larry.com/personal/my.index.html , his email is larry@briup. com, my other first is jacky, his telephoen is 17751229900',
groups: undefined
]
[
'http://www.larry.com/personal/my',
'http',
'www.larry.com',
undefined,
'www.larry.com',
undefined,
'/personal/my',
index: 206,
input: 'hello , i am terry, my website is http://134.175.154.93:8888/personal/index.html, ftp url is ftp://172.16.0.20/webui , my phone 18812345432, my friend is larry ,his telephone is 13710009999,his web site is http://www.larry.com/personal/my.index.html , his email is larry@briup. com, my other first is jacky, his telephoen is 17751229900',
groups: undefined
]
總結
這篇是我在學習js正則時總結的一些基礎,掌握!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/162617.html
標籤:其他
