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

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

 更新時間: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) 

知識點:

exec() 是RegExp對象的方法,而 match() 是String對象的方法。
都會返回包含第一個匹配項信息的數(shù)組;或者在沒有匹配項的情況下返回null。
返回的數(shù)組雖然是Array 的實例,但包含兩個額外的屬性:index 和 input。其中,index 表示匹配項在字符串中的位置,而 input 表示應用正則表達式的字符串。
在數(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() 找到一個或多個正則表達式的匹配。
replace() 替換與正則表達式匹配的子串。
search() 檢索與正則表達式相匹配的值。

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

相關(guān)文章

最新評論

博客| 红安县| 白河县| 防城港市| 平遥县| 长顺县| 汉寿县| 平定县| 平南县| 塔河县| 阳信县| 治多县| 安庆市| 黔西县| 杂多县| 滦平县| 阿巴嘎旗| 绥棱县| 奉贤区| 涟水县| 台山市| 布拖县| 白朗县| 阿拉善盟| 寿阳县| 德钦县| 岳池县| 东莞市| 平潭县| 潞城市| 肃北| 青铜峡市| 霸州市| 连州市| 柘城县| 靖江市| 察雅县| 溧水县| 雅安市| 佛学| 泸水县|