正則匹配某字符開始某字符結束並替換

結論

a.*?b 就是 a 開始 b 結束的匹配
若是要限制是一行的開頭和末尾的話,就是 ^a.*?b$javascript

實例

原字符java

aaa javascript:LUI.pageOpen('/ekp/sys/profile/index.jsp#app/ekp/km/review','_blank'); ccc

或者app

aaa javascript:LUI.pageOpen('/ekp/sys/profile/index.jsp#app/ekp/sys/task','_blank'); ccc

須要替換成 aaa bbb ccccurl

<script type="text/javascript">
    var item = "aaa javascript:LUI.pageOpen('/ekp/sys/profile/index.jsp#app/ekp/km/review','_blank'); ccc";
    // var item = aaa javascript:LUI.pageOpen('/ekp/sys/profile/index.jsp#app/ekp/sys/task','_blank'); ccc
    var curl = window.location.href;
    var replaceText = "javascript:showTipWindow('"+curl+"');";
    var result = item.replace(/javascript:LUI.pageOpen\(\'\/.*sys\/profile.*?_blank\'\);/,replaceText);
    document.write(result);

    window.showTipWindow = function(url){
        alert(url);
    }
</script>
相關文章
相關標籤/搜索