2017-07-20 70 views
1

我想先說我對javascript非常新,並且基本上做了大量的谷歌搜索以找到這個地方和各種其他資源。雖然我找到了一個腳本來修改我想要的東西(並設法讓它起作用),但它會干擾指定div內的任何鏈接。有沒有一種方法可以從JavaScript中排除鏈接,只是JavaScript會影響文本?從Javascript中排除鏈接

這是javascript。雖然我沒有問題的第一部分工作(我替換引用文本),我似乎無法排除鏈接和圖像,其中有引號的html。

$("div.gam-posttem div").each(function(){ 
    this.innerHTML = this.innerHTML.replace(/\"([^\"]+)\"/g, 
     '<div class="gam-quotetext">"$1"</div>'); 
}); 
$not('div.gam-posttem > div > a').each(function(){}); 

這裏是我使用的html。

<div class="gam-posttem"><div class="gam-posttem1"> 

"quote here" and regular text here. "more quote here" 

<br><br><a href="http://www.google.com">Link is Here</a> 

</div></div> 

任何幫助是非常感謝,如果你需要任何更多的信息,如CSS,請隨時問。

+0

你到底要什麼樣的JavaScript與您聯繫嗎? – Vedant

+0

可以更新正則表達式來避免標籤內部的文字,否則使用內部循環跳過標籤 – Bug

回答

0

是什麼讓這個特別棘手的是JavaScript正則表達式不允許後退。你想要做的就是嘗試和匹配「對,其中有相同數量的<和>字符(在其他」字符之外「)之前。而且,即使你可以,那會是一個非常討厭的正則表達式...

但是,你只在乎外面的字符<>對,其中使用正則表達式(雖然不建議)匹配是可能的: <(?:[^><\"\']*?(?:([\"\']).*?\1)?[^><\'\"]*?)+(?:>|$)會匹配所有<>對,忽略標記內引號內的尖括號。因此,您希望匹配這些標籤之外的所有內容。

有可能是一個更好的方式來做到這一點,但你可以嘗試以下的想法:

  1. 匹配的所有標籤
  2. 對於每一個標籤,讓起始位置和長度=>計算終點位置
  3. 字符串的起始索引添加到末尾位置
  4. 前部的字符串的長度添加到開始位置
  5. 爲每個(端,啓動)對(因爲我們正在反轉匹配),運行替換方法並修改字符串。

String.prototype.spliceish = function(start, end, newSubStr) { 
 
    return this.slice(0, start) + newSubStr + this.slice(end); 
 
}; 
 

 
var tagMatch = /<(?:[^><\"\']*?(?:([\"\']).*?\1)?[^><\'\"]*?)+(?:>|$)/g; 
 
var tokenMatch = /\"([^\"]+)\"/g; 
 

 
function invertedMatch(htmlString) { 
 
\t var returnString = htmlString; 
 
\t var startIndexes = [], 
 
    \t lengths = [], 
 
    match = tagMatch.exec(htmlString); 
 
    while(match !== null) 
 
    { 
 
    \t startIndexes.push(match.index); 
 
    lengths.push(match[0].length); 
 
    match = tagMatch.exec(htmlString); 
 
    } 
 
    
 
    var endIndexes = startIndexes.map(function(val, ix) { return val + lengths[ix]; }); 
 
    var invertedStarts = [0].concat(endIndexes); // we are inverting, so capture start text. 
 
    var invertedEnds = startIndexes.concat(htmlString.length); 
 
    
 
    // will need to go backwards 
 
    for(var i = invertedStarts.length - 1; i >= 0; i--) { 
 
    \t var start = invertedStarts[i], 
 
    \t end = invertedEnds[i]; 
 
    \t var stringReplace = htmlString.substring(start, end); 
 
    returnString = returnString.spliceish(start, end, stringReplace.replace(tokenMatch, 
 
    \t '<div class="gam-quotetext">"$1"</div>')); 
 
    }; 
 
    
 
    return returnString; 
 
} 
 

 
$("#root").html(invertedMatch($("#root").html()));
.gam-quotetext{ 
 
    color: green 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="root"> 
 
    <div class="gam-posttem"> 
 
    <div class="gam-posttem1"> 
 

 
    "quote here" and regular text here. "more quote here" 
 

 
    <br> 
 
    <br> 
 
    <a href="http://www.google.com">Link is Here</a> 
 

 
    </div> 
 
    </div> 
 
</div>

+0

謝謝!但是,雖然我可以在JSFiddle中完美地工作,但我似乎無法在我的網站上工作,這是一個Jcink論壇。我是否將javascript包裝在簡單的