国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 湄潭县| 郸城县| 和田县| 平顶山市| 忻城县| 调兵山市| 遂溪县| 孝义市| 托克逊县| 台南县| 垫江县| 盐城市| 红桥区| 聂拉木县| 汉源县| 永修县| 连云港市| 西平县| 敖汉旗| 红桥区| 庄浪县| 新兴县| 当涂县| 南安市| 英吉沙县| 龙口市| 托克逊县| 东台市| 四川省| 台湾省| 揭阳市| 利川市| 壤塘县| 南部县| 桓仁| 榆林市| 姜堰市| 长顺县| 新民市| 珠海市| 南阳市|