Home > Web Front-end > JS Tutorial > JavaScript uses regular expressions to replace content in strings

JavaScript uses regular expressions to replace content in strings

高洛峰
Release: 2017-01-14 09:29:33
Original
1388 people have browsed it

Not much to say, please look at the specific implementation code

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


The above is the entire content of this article. I hope that the content of this article can bring some benefits to everyone's study or work. For help, I also hope to support PHP Chinese website!

For more JavaScript using regular expressions to replace content in strings, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template