JS 截取两个字符串之间的内容:
- var str = "aaabbbcccdddeeefff";
- str = str.match(/aaa(\S*)fff/)[1];
- alert(str);// 结果 bbbcccdddeee
JS 截取某个字符串前面的内容:
- var str = "aaabbbcccdddeeefff";
- tr = str.match(/(\S*)fff/)[1];
- alert(str);// 结果 aaabbbcccddd
JS 截取某个字符串后面的内容:
- var str = "aaabbbcccdddeeefff";
- str = str.match(/aaa(\S*)/)[1];
- alert(str);// 结果 bbbcccdddeeefff
JS 利用正则表达式替换字符串中的内容 https://www.cnblogs.com/cao-zhen/p/6160670.html :
- // 从字符串'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
来源: http://www.bubuko.com/infodetail-2988090.html