最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年03月21日 17:30:08   作者:oSHYo  
這篇文章主要介紹了關(guān)于JS中match() 和 exec() 返回值和屬性的測試 的相關(guān)資料,需要的朋友可以參考下

語法:

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

知識點(diǎn):

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

測試:

對 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() 找到一個(gè)或多個(gè)正則表達(dá)式的匹配。
replace() 替換與正則表達(dá)式匹配的子串。
search() 檢索與正則表達(dá)式相匹配的值。

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

相關(guān)文章

最新評論

镇远县| 大连市| 白银市| 通渭县| 华容县| 正定县| 盐亭县| 孝义市| 扬州市| 安义县| 乐业县| 石家庄市| 肥乡县| 邹城市| 夏河县| 上饶市| 田阳县| 松滋市| 清镇市| 武冈市| 凤凰县| 宝山区| 亳州市| 莱州市| 金湖县| 疏附县| 金塔县| 陇川县| 衢州市| 金沙县| 溧阳市| 吉隆县| 措勤县| 图们市| 东方市| 常德市| 都兰县| 鱼台县| 蒙山县| 平邑县| 南雄市|