...
//從字元串'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