JavaScript利用正则表达式替换字符串中的内容
话不多说,请看具体实现代码
//从字符串'Is this all there is'中剪去'is': var str='Is this all there is'; var subStr=new RegExp('is');//创建正则表达式对象 var result=str.replace(subStr,"");//把'is'替换为空字符串 console.log(result);//Is th all there is var subStr=new RegExp('is','i');//创建正则表达式对象,不区分大小写 var result=str.replace(subStr,"");//把'is'替换为空字符串 console.log(result);//this all there is var subStr=new RegExp('is','ig');//创建正则表达式对象,不区分大小写,全局查找 var result=str.replace(subStr,"");//把'is'替换为空字符串 console.log(result);//th all there var subStr=/is/ig;//直接量法创建正则表达式对象,不区分大小写,全局查找 var result=str.replace(subStr,"");//把'is'替换为空字符串 console.log(result);//th all there console.log(str);//Is this all there is 可见replace并不改变原始str
相关推荐
eroshn 2020-06-21
wangzhaotongalex 2020-10-20
wyq 2020-11-11
TLROJE 2020-10-26
风雨断肠人 2020-10-13
duanqingfeng 2020-09-29
rechanel 2020-11-16
cshanzhizi 2020-10-16
luofuIT成长记录 2020-09-22
phphub 2020-09-10
taomengxing 2020-09-07
MaggieRose 2020-08-19
flyingssky 2020-08-18
山水沐光 2020-08-18
jyj00 2020-08-15
AHuqihua 2020-08-09