正則是獨立於程式語言的一個學科,用於解決模式匹配問題,Javascript提供了對於正則支持,此外,Java、c、python也都支持正則。正則可以應用在:檢索,替換,爬蟲,論文查重等領域。
實例化正則表達式對象
正則表達式
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
/() : //(()|()): ()()/
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/gimconsole.log("source:",pattern.source);// source: helloconsole.log("flags:",pattern.flags);// flags: gimconsole.log("ignoreCase:",pattern.ignoreCase);// ignoreCase: trueconsole.log("global:",pattern.global);// global: trueconsole.log("multiline:",pattern.multiline);// multiline: trueconsole.log("unicode:",pattern.unicode);// unicode: false123456789
對了,在這裡說一下,我目前是在職web前端開發,如果你現在正在學習前端,了解前端,渴望成為一名合格的web前端開發工程師,在入門學習前端的過程當中有遇見任何關於學習方法,學習路線,學習效率等方面的問題,都可以隨時關注並私信我:前端,我都會根據大家的問題給出針對性的建議,缺乏基礎入門的視頻教程也可以直接來找我,我這邊有最新的web前端基礎精講視頻教程, 還有我做web前端技術這段時間整理的一些學習手冊,面試題,開發工具,PDF文檔書籍教程,都可以直接分享給大家。
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.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";//網址的正則表達式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,})/iglet result = null;while(result = a.exec(str)){ console.log(result);}1234567891011
輸出的結果:
[ '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]123456789101112131415161718192021222324252627282930313233343536
總結
這篇是我在學習js正則時總結的一些基礎,掌握!
原文連結:https://blog.csdn.net/wt1310445488/article/details/108939299?utm_medium=distribute.pc_category.none-task-blog-hot-5.nonecase&depth_1-utm_source=distribute.pc_category.none-task-blog-hot-5.nonecase&request_id=
作者:wt1310445488