国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

關(guān)于JS中match() 和 exec() 返回值和屬性的測試

2019-11-20 10:21:51
字體:
供稿:網(wǎng)友

語法:

exec() :RegExpObject.exec(string) match() :stringObject.match(string)stringObject.match(regexp) 

知識點:

exec() 是RegExp對象的方法,而 match() 是String對象的方法。
都會返回包含第一個匹配項信息的數(shù)組;或者在沒有匹配項的情況下返回null。
返回的數(shù)組雖然是Array 的實例,但包含兩個額外的屬性:index 和 input。其中,index 表示匹配項在字符串中的位置,而 input 表示應(yīng)用正則表達(dá)式的字符串。
在數(shù)組中,第一項是與整個模式匹配的字符串,其他項是與模式中的捕獲組匹配的字符串(如果模式中沒有捕獲組,則該數(shù)組只包含一項)。

測試:

對 match() 的測試代碼:

var text = "mom and dad and baby";var pattern = /(mom and )?(dad and )?baby/;var matches = text.match(pattern);//pattern.exec(text);console.log(matches.index);console.log(matches.input);console.log(matches[0]);console.log(matches[1]);console.log(matches[2]);

對 match() 的測試結(jié)果截圖:


對 exec() 的測試代碼:

var text = "mom and dad and baby";var pattern = /(mom and )?(dad and )?baby/;var matches = pattern.exec(text);//text.match(pattern);console.log(matches.index);console.log(matches.input);console.log(matches[0]);console.log(matches[1]);console.log(matches[2]);

對 exec() 的測試結(jié)果截圖:


String 對象方法

方法 描述
exec 檢索字符串中指定的值。返回找到的值,并確定其位置
test 檢索字符串中指定的值。返回 true 或 false。

String 對象方法

方法 描述
match() 找到一個或多個正則表達(dá)式的匹配。
replace() 替換與正則表達(dá)式匹配的子串。
search() 檢索與正則表達(dá)式相匹配的值。

關(guān)于JS中match() 和 exec() 返回值和屬性的測試就給大家介紹到這里,希望對大家有所幫助!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁海县| 含山县| 竹溪县| 南安市| 林甸县| 沙坪坝区| 万州区| 台南市| 冷水江市| 图木舒克市| 灵武市| 洪洞县| 田东县| 兴国县| 淮北市| 大城县| 项城市| 拜泉县| 斗六市| 梁山县| 乌审旗| 安陆市| 佳木斯市| 高尔夫| 呼玛县| 金沙县| 舒兰市| 枞阳县| 建平县| 绍兴市| 海宁市| 积石山| 达日县| 陕西省| 利津县| 榆社县| 常州市| 崇州市| 桓仁| 桓仁| 炉霍县|