- split()可以将一个字符串拆分为数组,可以传递正则表达式作为参数
- 默认全局
var result = str.split("c");
console.log(result);
result = str.split(/[A-z]/);
console.log(result);
- search()可以搜索字符串中是否含有指定内容
- 如果搜索到,返回下标,否则返回-1
- 可以接受正则表达式作为参数,然后根据正则表达式去检索字符串
- 即使设置全局匹配,也只会查找一个
str = "hello abc hello abc";
result = str.search(/a[bef]c/);
console.log(result);
- match()可以根据正则表达式,从字符串中提取符合条件的内容
- 默认情况下,只找第一个符合条件的式子
- 可以为一个正则表达式设置多个匹配模式,顺序无所谓
- 匹配的的内容封装到数组中返回
str = "1a2b3c4d5e6f7";
result = str.match(/[A-z]/);
console.log(result);
result = str.match(/[A-z]/g);
console.log(result);
- replace()可以将字符串中指定内容替换为新的内容
- 参数:
1.被替换内容,可以接受正则表达式
2.新的内容 - 默认只会替换第一个
str = "1a2a3A4d5e6f7";
result = str.replace(/a/ig,"@_@");
console.log(result);