淺談JavaScript -- Regex

來源:互聯網
上載者:User

標籤:參數   return   java   全域   console   lse   解決方案   odi   表達   

什麼是Regex?

Regex是由一個字元序列形成的搜尋模式。可用於文本搜尋和文本替換。

文法:/Regex主體/修飾符(可選)

var patt=new RegExp(pattern,modifiers);或者更簡單的方式:var patt=/pattern/modifiers;
Regex修飾符
修飾符 描述
i 執行對大小寫不敏感的匹配。
g 行全域匹配(尋找所有匹配而非在找到第一個匹配後停止)。
m 執行多行匹配。
Regex模式
運算式 描述
[abc] 尋找方括弧之間的任何字元。
[^abc] 尋找方括弧以外的任何字元。
[0-9] 尋找任何從 0 至 9 的數字。
(x|y) 尋找任何以 | 分隔的選項。
{n,m} 匹配長度。
. 任何單個字元。
\ 逸出字元。
量詞:
量詞 描述
n+ 匹配任何包含至少一個 n 的字串。
n* 匹配任何包含零個或多個 n 的字串。
n? 匹配任何包含零個或一個 n 的字串。
?=n 匹配任何其後緊接指定字串 n 的字串。即n前面的。
?!n 匹配任何其後沒有緊接指定字串 n 的字串。
var str="name=adoctors;age=24;";console.log(str.match(/[^=;]+(?==)/g));//[ ‘name‘, ‘age‘ ]console.log(str.match(/[^=;]+(?!=)/g));//[ ‘nam‘, ‘adoctors‘, ‘ag‘, ‘24‘ ]
RegExp 對象方法
方法 描述
test 檢索字串中指定的值。返回 true 或 false。
exec 檢索字串中指定的值。返回找到的值,並確定其位置。沒有匹配到返回null。

test方法使用g時,兩側使用時,會出現lastIndex問題。

var str=‘hello‘,patt=/e/g;function fun(){    console.log(patt.lastIndex);    console.log(patt.test(str));}fun();  //0,truefun();  //2,falsefun();  //0,truefun();  //2,false//解決方案:1、不用g2、在第二次使用前設定patt.lastIndex=0;即可。
支援Regex的string對象的方法
方法 描述
match 檢索字串中指定的值。找到一個或多個匹配到的結果,並輸出一個數組。沒有匹配到返回null。
replace 用指定的值替換對應的值。
split 將字串分割成數組。

例:在一段文本的每個行首跟行尾加上p標籤

var str=‘hello adoctors!‘;var patt=/o/gi;console.log(str.match(patt));       //[ ‘o‘, ‘o‘, ‘o‘ ]console.log(str.split(patt));       //[ ‘hell‘, ‘ ad‘, ‘ct‘, ‘rs!‘ ]//split第二個參數設定返回的數組的最大長度console.log(str.split(patt,2));       //[ ‘hell‘, ‘ ad‘]var strs=`aaaa\nbbbbbbbbbbbb\ncccccc\n`;var strs2=str.replace(/^/gm,‘<p>‘).replace(/$/gm,‘</p>‘);//replace方法第二個參數可以是函數let str3=str.replace(patt,function($0,$1,$2){    console.log($0) //匹配到的元素      o,o,o    console.log($1) //本次匹配到元素對應的位置  //4,8,11    console.log($2) //要匹配的目標字串    //hello adoctors!,hello adoctors!,hello adoctors!    return 2;})console.log(str3)   //hell2 ad2ct2rs!console.log(str)   //hello adoctors!
常用的Regex:
26個大寫字母    [A-Z]26個小寫字母    [a-z]0-9數字         [0-9]   或\d    當字母為大寫是為非:\W非數字空白字元        \s                                  \S非空包括大小寫字母,底線,0-9 [A-Za-z0-9_]  或\w匹配中文:[\u4e00-\u9fa5] //中文ACALL碼的範圍行首行尾空格:^\s*|\s*$ //首行出現任意個空格或者尾行出現任意個空格(任意表示也可以沒有空格)檢測密碼強度正則/^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[[email protected]#$%^&*? ]).*$///去除html標籤行內樣式var style=`<span style="color:red;">hello adoctors!</span>`;var pat=/style=\"(.*)\"/g;console.log(style.replace(pat,‘‘));     //<span >hello adoctors!</span>

淺談JavaScript -- Regex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.