var str1="The rain in SPAIN stays mainly in the plain";var str2 = str1.match(/ain/gi)// 全局查找字符串 "ain",且不区分大小写var str3 = str1.match(/hello/gi)
console.log(str1)// The rain in SPAIN stays mainly in the plain
console.log(str2)// ["ain", "AIN", "ain", "ain"] 返回由元素组成的数组
console.log(str3)// null
replace()替换与正则表达式匹配的子串
var str1="Mr Blue has a blue house and a blue car";var str2 = str1.replace(/blue/gim,"red");// g执行全局匹配(查找所有匹配而非在找到第一个匹配后停止),i执行对大小写不敏感的匹配,m执行多行匹配。var str3 = str1.replace(/hello/gim,"red")
console.log(str1)// Mr Blue has a blue house and a blue car
console.log(str2)// Mr red has a red house and a red car
console.log(str3)// Mr Blue has a blue house and a blue car
search()检索与正则表达式相匹配的值返回匹配到的下标
var str1="Mr Blue has a blue house and a blue car";var str2 = str1.search(/blue/gim,"red");// g执行全局匹配(查找所有匹配而非在找到第一个匹配后停止),i执行对大小写不敏感的匹配,m执行多行匹配。var str3 = str1.search(/hello/gim,"red")
console.log(str1)// Mr Blue has a blue house and a blue car
console.log(str2)//3
console.log(str3)//-1
slice()提取字符串的片断,并在新的字符串中返回被提取的部分,包括开始下标,不包含结束下标
var str ="hello world"var str1 = str.slice(3)var str2 = str.slice(3,8)
console.log(str)//hello world
console.log(str1)//lo world
console.log(str2)//lo wo
split()把字符串通过某个字符分割为子字符串数组
var str ="How are you doing today"var str1 = str.split(" ")
console.log(str)//How are you doing today
console.log(str1)//["How", "are", "you", "doing", "today"]
substr()从起始索引号提取字符串中指定数目的字符,从开始下标截取n个字符
var str ="hello world"var str1 = str.substr(3)var str2 = str.substr(3,4)
console.log(str)//hello world
console.log(str1)//lo world
console.log(str2)//lo w
var str ="hello world"var str1 = str.substring(3)var str2 = str.substring(3,8)
console.log(str)//hello world
console.log(str1)//lo world
console.log(str2)//lo wo