富贵资源网 Design By www.hznty.com

话不多说,请看具体实现代码

//从字符串'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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!

富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com