search():检测母串中是否含有指定的子串(正则表达式),如果含有,返回该子串的位置,否则返回-1
语法:字符串.search(RegExp)
match():检测母串中是否含有知道的子串(正则表达式),如果含有,返回该子串的数组,否则返回空字符
语法:字符串.match(RegExp)
数组[检测到的子串,子串在母串中索引,母串]
replace():用新的字符串替换正则字符串
语法:字符串.replace(RegExp,新的子串)
split():按照指定的字符串分割母串味数组
语法:字符串.split(RegExp)
例子:
search()
var str = "vvvvvavvvvv";
str = str.search(/a/);
console.log(str,typeof str); //输出结果为 5 number
match()
var str = "vvvAvvAvvvAvv";
str = str.match(/A/g);
console.log(str,typeof str); //输出结果为 ["A", "A", "A"] object
replace()
var str = "vvvAvvAvvvAvv";
str = str.replace(/A/g,'O');
console.log(str,typeof str); //输出结果为 vvvOvvOvvvOvv string
split()
var str = "somgthing just like this";
str = str.split(/\s/); // \s代表空格,字符串按空格分割为数组
console.log(str,typeof str); //输出结果为 ["somgthing", "just", "like", "this"] object