固然,這個查找替換功能能夠選擇不選中正則表達式。javascript
兩個關鍵函數(寫了三小時才測試成功),要來得瑟一下!勾選中了「正則?」以後,能夠實現跨行搜索內容。java
function find(isdesc){ var startPos=Editor.selectionStart,endPos=Editor.selectionEnd; var isreg=document.se.isreg.checked; var str=document.se.find.value; if(str=='')return false; var tmpStr=Editor.value,len=str.length,ss=-1; if(tmpStr=='')return false; //console.log(startPos+','+endPos+','+tmpStr.length); if(!isdesc){ if(startPos==tmpStr.length)startPos=Editor.selectionStart=endPos=Editor.selectionEnd=0; if(isreg){ var reg=new RegExp(str,"im"); tmpStr=tmpStr.substr(endPos); var arr=tmpStr.match(reg); if(arr){str=arr[0];len=str.length;ss=tmpStr.indexOf(str)+endPos;} } else{ ss=tmpStr.indexOf(str,endPos); } if(ss>-1){Editor.selectionStart=ss;Editor.selectionEnd=ss+len;}else {alert('已搜到底部');Editor.selectionStart=Editor.selectionEnd=0;} } else{ if(endPos==0)startPos=Editor.selectionStart=endPos=Editor.selectionEnd=tmpStr.length; if(isreg){ var reg=new RegExp(str,"img"); tmpStr=tmpStr.substring(0,startPos); var arr=tmpStr.match(reg); if(arr){ str=arr[arr.length-1]; len=str.length; ss=tmpStr.lastIndexOf(str); } } else{ ss=tmpStr.lastIndexOf(str,startPos-len); } if(ss>-1){Editor.selectionStart=ss;Editor.selectionEnd=ss+len;}else{alert('已搜到頂部');Editor.selectionStart=Editor.selectionEnd=tmpStr.length;} } }
function replace(isall){ var str=document.se.find.value,str2=document.se.for.value; var isreg=document.se.isreg.checked; if(str=='')return false; var tmpStr=Editor.value,len=str.length,ss=-1; if(tmpStr=='')return false; if(isall){ var pattern=str; if(!isreg)pattern=str.replace(/([\/\|\.\\\(\)\*\?\^\$])/g,'\\$1'); Editor.value=tmpStr.replace(new RegExp(pattern,"igm"),str2); // i:不區分大小寫,g:全局搜索,m:多行搜索 Editor.selectionStart=Editor.selectionEnd=0; } else{ var startPos=Editor.selectionStart,endPos=Editor.selectionEnd; if(isreg){ var reg=new RegExp(str,"im"); tmpStr=tmpStr.substr(startPos); var arr=tmpStr.match(reg); if(arr){str=arr[0];len=str.length;ss=tmpStr.indexOf(str)+startPos;str2=str.replace(reg,str2);} } else ss=tmpStr.indexOf(str,startPos); if(ss>-1){ tmpStr=Editor.value; Editor.value=tmpStr.substring(0,ss)+str2+tmpStr.substr(ss+str.length); Editor.selectionStart=ss;Editor.selectionEnd=ss+str2.length;}else {alert('已搜到底部');Editor.selectionStart=Editor.selectionEnd=0; } } }
如今我正在翻譯SVG2.0的w3c草案。正則表達式