1. 程式人生 > >js 正則表達式 的匹配查找,替換,匹配兩個特定字符間之前之後的內容

js 正則表達式 的匹配查找,替換,匹配兩個特定字符間之前之後的內容

ron regexp pos www 直接 查找 正則表達 fff 替換字符

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利用正則表達式替換字符串中的內容:

//從字符串‘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

js 正則表達式 的匹配查找,替換,匹配兩個特定字符間之前之後的內容