正則表達(dá)式對(duì)象的方法
1、test,返回一個(gè) Boolean 值,它指出在被查找的字符串中是否存在模式。如果存在則返回 true,否則就返回 false。
2、exec,用正則表達(dá)式模式在字符串中運(yùn)行查找,并返回包含該查找結(jié)果的一個(gè)數(shù)組。
3、compile,把正則表達(dá)式編譯為內(nèi)部格式,從而執(zhí)行得更快。
正則表達(dá)式對(duì)象的屬性
1、source,返回正則表達(dá)式模式的文本的復(fù)本。只讀。
2、lastIndex,返回字符位置,它是被查找字符串中下一次成功匹配的開(kāi)始位置。
3、input ($_),返回執(zhí)行規(guī)范表述查找的字符串。只讀。
4、lastMatch ($&),返回任何正則表達(dá)式搜索過(guò)程中的最后匹配的字符。只讀。
5、lastParen ($+),如果有的話(huà),返回任何正則表達(dá)式查找過(guò)程中最后括的子匹配。只讀。
6、leftContext ($`),返回被查找的字符串中從字符串開(kāi)始位置到最后匹配之前的位置之間的字符。只讀。
7、rightContext ($'),返回被搜索的字符串中從最后一個(gè)匹配位置開(kāi)始到字符串結(jié)尾之間的字符。只讀。
String對(duì)象一些和正則表達(dá)式相關(guān)的方法
1、match,找到一個(gè)或多個(gè)正則表達(dá)式的匹配。
2、replace,替換與正則表達(dá)式匹配的子串。
3、search,檢索與正則表達(dá)式相匹配的值。
4、split,把字符串分割為字符串?dāng)?shù)組。
案例1 test方法測(cè)試
//test方法,測(cè)試字符串,符合模式時(shí)返回true,否則返回false var re = /he/;//最簡(jiǎn)單的正則表達(dá)式,將匹配he這個(gè)單詞 var str = "he"; console.log(re.test(str));//true str = "we"; console.log(re.test(str));//false str = "HE"; console.log(re.test(str));//false,大寫(xiě),如果要大小寫(xiě)都匹配可以指定i標(biāo)志(i是ignoreCase或case-insensitive的表示) re = /he/i; console.log(re.test(str));//true str = "Certainly!He loves her!"; console.log(re.test(str));//true,只要包含he(HE)就符合,如果要只是he或HE,不能有其它字符,則可使用^和$ re = /^he/i;//脫字符(^)代表字符開(kāi)始位置 console.log(re.test(str));//false,因?yàn)閔e不在str最開(kāi)始 str = "He is a good boy!"; console.log(re.test(str));//true,He是字符開(kāi)始位置,還需要使用$ re = /^he$/i;//$表示字符結(jié)束位置 console.log(re.test(str));//false str = "He"; console.log(re.test(str));//true //當(dāng)然,這樣不能發(fā)現(xiàn)正則表達(dá)式有多強(qiáng)大,因?yàn)槲覀兺耆梢栽谏厦娴睦又惺褂?=或indexOf re = //s/;// /s匹配任何空白字符,包括空格、制表符、換頁(yè)符等等 str= "user Name";//用戶(hù)名包含空格 console.log(re.test(str));//true str = "user Name";//用戶(hù)名包含制表符 console.log(re.test(str));//true re=/^[a-z]/i;//[]匹配指定范圍內(nèi)的任意字符,這里將匹配英文字母,不區(qū)分大小寫(xiě) str="variableName";//變量名必須以字母開(kāi)頭 console.log(re.test(str));//true str="123abc"; console.log(re.test(str));//false
案例2 exec測(cè)試
var haoVersion = "Haorooms 8";//其中的8表示系統(tǒng)主版本號(hào) var re = /^[a-z]+/s+/d+$/i; //+號(hào)表示字符至少要出現(xiàn)1次,/s表示空白字符,/d表示一個(gè)數(shù)字 console.log(re.test(haoVersion));//true,但我們想知道主版本號(hào) //另一個(gè)方法exec,返回一個(gè)數(shù)組,數(shù)組的第一個(gè)元素為完整的匹配內(nèi)容 re=/^[a-z]+/s+/d+$/i; arr = re.exec(haoVersion); console.log(arr[0]);//將haoVersion完整輸出,因?yàn)檎麄€(gè)字符串剛好匹配re //我只需要取出數(shù)字 re=//d+/; var arr = re.exec(haoVersion); console.log(arr[0]);//8 //exec返回的數(shù)組第1到n元素中包含的是匹配中出現(xiàn)的任意一個(gè)子匹配 re=/^[a-z]+/s+(/d+)$/i;//用()來(lái)創(chuàng)建子匹配 arr =re.exec(haoVersion); console.log(arr[0]);//整個(gè)haoVersion,也就是正則表達(dá)式的完整匹配 console.log(arr[1]);//8,第一個(gè)子匹配,事實(shí)也可以這樣取出主版本號(hào) console.log(arr.length);//2 haoVersion = "Haorooms 8.10";//取出主版本號(hào)和次版本號(hào) re = /^[a-z]+/s+(/d+)/.(/d+)$/i;//.是正則表達(dá)式元字符之一,若要用它的字面意義須轉(zhuǎn)義 arr = re.exec(haoVersion); console.log(arr[0]);//完整的haoVersion console.log(arr[1]);//8 console.log(arr[2]);//10
案例3 String對(duì)象的一些和正則表達(dá)式有關(guān)的方法
1、關(guān)于replace,我之前的一片博客專(zhuān)門(mén)寫(xiě)了。還可以傳參數(shù)。
2、其他操作
//replace方法,用于替換字符串 var str ="some money"; console.log(str.replace("some","much"));//much money //replace的第一個(gè)參數(shù)可以為正則表達(dá)式 var re = //s/;//空白字符 console.log(str.replace(re,"%"));//some%money //在不知道字符串中有多少空白字符時(shí),正則表達(dá)式極為方便 str ="some some /tsome/t/f"; re = //s+/; console.log(str.replace(re,"#"));//但這樣只會(huì)將第一次出現(xiàn)的一堆空白字符替換掉 //因?yàn)橐粋€(gè)正則表達(dá)式只能進(jìn)行一次匹配,/s+匹配了第一個(gè)空格后就退出了 re = //s+/g;//g,全局標(biāo)志,將使正則表達(dá)式匹配整個(gè)字符串 console.log(str.replace(re,"@"));//some@some@some@ //另一個(gè)與之相似的是split var str = "a-bd-c"; var arr = str.split("-");//返回["a","bd","c"] //如果str是用戶(hù)輸入的,他可能輸入a-bd-c也可能輸入a bd c或a_bd_c,但不會(huì)是abdc(這樣就說(shuō)他輸錯(cuò)了) str = "a_db-c";//用戶(hù)以他喜歡的方式加分隔符s re=/[^a-z]/i;//前面我們說(shuō)^表示字符開(kāi)始,但在[]里它表示一個(gè)負(fù)字符集 //匹配任何不在指定范圍內(nèi)的任意字符,這里將匹配除字母處的所有字符 arr = str.split(re);//仍返回["a","bd","c"]; //在字符串中查找時(shí)我們常用indexOf,與之對(duì)應(yīng)用于正則查找的方法是search str = "My age is 18.Golden age!";//年齡不是一定的,我們用indexOf不能查找它的位置 re = //d+/; console.log(str.search(re));//返回查找到的字符串開(kāi)始下標(biāo)10 //注意,因?yàn)椴檎冶旧砭褪浅霈F(xiàn)第一次就立即返回,所以無(wú)需在search時(shí)使用g標(biāo)志 //下面的代碼雖然不出錯(cuò),但g標(biāo)志是多余的 re=//d+/g; console.log(str.search(re));//仍然是10 var str = "My name is CJ.Hello everyone!"; var re = /[A-Z]/;//匹配所有大寫(xiě)字母 var arr = str.match(re);//返回?cái)?shù)組 console.log(arr);//數(shù)組中只會(huì)包含一個(gè)M,因?yàn)槲覀儧](méi)有使用全局匹配 re = /[A-Z]/g; arr = str.match(re); console.log(arr);//M,C,J,H //從字符串中抽取單詞 re = //b[a-z]*/b/gi;///b表示單詞邊界 str = "one two three four"; console.log(str.match(re));//one,two,three,four
案例4 RegExp對(duì)象實(shí)例的一些屬性
var re = /[a-z]/i; console.log(re.source);//將[a-z]字符串輸出 //請(qǐng)注意,直接console.log(re)會(huì)將正則表達(dá)式連同前向斜線(xiàn)與標(biāo)志輸出,這是re.toString方法定義的 var re = /[A-Z]/; //exec方法執(zhí)行后,修改了re的lastIndex屬性, var str = "Hello,World!!!"; var arr = re.exec(str); console.log(re.lastIndex);//0,因?yàn)闆](méi)有設(shè)置全局標(biāo)志 re = /[A-Z]/g; arr = re.exec(str); console.log(re.lastIndex);//1 arr = re.exec(str); console.log(re.lastIndex);//7 var re = /[A-Z]/; var str = "Hello,World!!!"; re.lastIndex = 120; var arr = re.exec(str); console.log(re.lastIndex);//0
案例5 RegExp對(duì)象的靜態(tài)屬性
//input 最后用于匹配的字符串(傳遞給test,exec方法的字符串) var re = /[A-Z]/; var str = "Hello,World!!!"; var arr = re.exec(str); console.log(RegExp.input);//Hello,World!!! re.exec("tempstr"); console.log(RegExp.input);//仍然是Hello,World!!!,因?yàn)閠empstr不匹配 //lastMatch 最后匹配的字符 re = /[a-z]/g; str = "hi"; re.test(str); console.log(RegExp.lastMatch);//h re.test(str); console.log(RegExp["$&"]);//i ,$&是lastMatch的短名字,但由于它不是合法變量名,所以要。。 //lastParen 最后匹配的分組 re = /[a-z](/d+)/gi; str = "Class1 Class2 Class3"; re.test(str); console.log(RegExp.lastParen);//1 re.test(str); console.log(RegExp["$+"]);//2 //leftContext 返回被查找的字符串中從字符串開(kāi)始位置到最后匹配之前的位置之間的字符 //rigthContext 返回被搜索的字符串中從最后一個(gè)匹配位置開(kāi)始到字符串結(jié)尾之間的字符 re = /[A-Z]/g; str = "123ABC456"; re.test(str); console.log(RegExp.leftContext);//123 console.log(RegExp.rightContext);//BC456 re.test(str); console.log(RegExp["$`"]);//123A console.log(RegExp["$'"]);//C456
案例6 使用RegExp構(gòu)造函數(shù)注意點(diǎn)
var str = "/?"; console.log(str);//只會(huì)輸出? var re = //?/;//將匹配? console.log(re.test(str));//true re = new RegExp("/?");//出錯(cuò),因?yàn)樽址锩?是轉(zhuǎn)義字符/?相當(dāng)于?要得到/?,就要//? re = new RegExp("//?");//正確,將匹配? console.log(re.test(str));//true 在正則表達(dá)式中使用特殊字符//ASCII方式用十六進(jìn)制數(shù)來(lái)表示特殊字符 var re = /^/x43/x4A$/;//將匹配CJ console.log(re.test("CJ"));//true //也可使用八進(jìn)制方式 re = /^/103/112$/;//將匹配CJ console.log(re.test("CJ"));//true //還可以使用Unicode編碼 re =/^/u0043/u004A$/;//使用 Unicode,必須使用u開(kāi)頭,接著是字符編碼的四位16進(jìn)制表現(xiàn)形式 console.log(re.test("CJ"));
以上就是關(guān)于五個(gè)常用函數(shù)的簡(jiǎn)單應(yīng)用,希望對(duì)大家的學(xué)習(xí)有所幫助。