標題可以很好地解釋它。我想在JavaScript中每次出現特定單詞(例如:'the')後添加用戶定義的單詞或短語(例如:'great')。所以每當'the'出現在HTML的文本中(等等等等),JavaScript就會注入'最大'(等等等等等等)。我試圖用正則表達式來解決這個問題,但沒有直接將它添加到HTML中。在某個單詞的每個實例之後搜索HTML和短語
0
A
回答
0
事實上,正則表達式是完美的做到這一點。您可以使用String.replace()函數替換文本:
someString.replace(/\b(the)\b/ig, "$1 greatest");
這裏,\b
代表單詞邊界(空間,時間等),並避免與「行吟詩人最大的R」,周圍的括號「代替「其他」 「允許您選擇它(在更換中使用它,通過使用$1
)。然後,i
和g
是使您的正則表達式不區分大小寫和全局(標識多次出現)的標誌。
但這不是大部分工作需要完成的地方。替換元素的文本可能會干擾實際的HTML。爲了避免這種情況,你可以遞歸函數只定位文本節點:
document.getElementById('btn').addEventListener('click', function() {
doReplace(/\b(the)\b/ig, "$1 greatest", document.body);
this.style.visibility = 'hidden';
});
function doReplace(search, replacement, element) {
if (element.nodeType == 3) { // if the element is a text node, replace the content
element.nodeValue = element.nodeValue.replace(search, replacement);
} else { // otherwise, apply this function to all children
var children = element.childNodes;
for (var i = 0; i < children.length; i++) {
doReplace(search, replacement, children[i]);
}
}
}
<button id="btn">Add "greatest"!</button>
<h1>The pie was cold</h1>
<p>I was at the restaurant the other day, and the waiter took my order. The problem? My pie was cold!</p>
0
這是一些代碼,將做你想做的。請注意,它不會更改「The orange」,因爲它是大小寫敏感的。還要注意,它不會更改提醒的「橙色」,因爲它存在於腳本元素中。
function getTextNodesThatContain(text) {
var textNodes = $(document).find(":not(iframe, script, style)")
.contents().filter(
function() {
return this.nodeType == 3
&& this.textContent.indexOf(text) > -1;
});
return textNodes;
};
$(document).ready(function() {
getTextNodesThatContain("the").each(function(ind, item) {
item.nodeValue = item.nodeValue.replace("the", "the greatest");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
This is the apple.
<div> this is the orange. </div>
<script>
function alertme() {
alert("the orange");
}
</script>
<a href="javascript:alertme()">The orange</a>
0
我的建議是使用TreeWalker搜索身體內的所有文本節點:
function replaceTextInPage(el, txtMatch, txtToReplace) {
txtToReplace = (txtToReplace.length > 0) ? ' ' + txtToReplace : '';
var walk = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, null, false);
while (n = walk.nextNode()) {
if (n.textContent.trim().length > 0) {
n.textContent = n.textContent.replace(new RegExp(txtMatch, "gi"), txtMatch + txtToReplace);
}
}
}
window.onload = function() {
document.getElementById('btn').addEventListener('click', function() {
replaceTextInPage(document.body, document.getElementById('txtToFind').value, document.getElementById('txtToAdd').value)
}, false);
}
<button id="btn">Replace string</button><br/>
Text to search: <input id="txtToFind" type="text" value="the"><br/>
Text to add at the end: <input id="txtToAdd" type="text" value="greatest"><br/>
<div style="width: 100%;height: 100px;">It seems to only remove <span style="color: red;">the</span> first occurrence of abc in <span style="color: red;">the</span> string above. How can I replace all occurrences of it?</div>
+0
我從來沒有聽說過treewalker。這是我必須考慮的事情。 – Zuse
0
要動態地完成這項工作, y正在發明我們自己的String.prototype.affix()
方法。這個新方法將做你想做的事情,如果最後一個參數是true
它附加提供的字符串來粘貼(第二個參數)到最後,如果false
到搜索到的一段子字符串(第一個參數)的前面。我已將true
作爲默認值分配給最後一個參數。
String.prototype.affix = function(s,a,b = true){
var r = new RegExp(s,"g");
return b ? this.replace(r,"$&" + " " + a)
: this.replace(r,a + " " + "$&");
}
console.log("this is the book and the author is him".affix("the", "greatest",true));
相關問題
- 1. scala:正則表達式用於查找x個單詞之前和x個單詞之後的某個短語
- 2. 用某些詞彙打印每個短語/單詞的頻率?
- 3. 搜索並找到一個短語並獲取它之前和之後的兩個單詞
- 4. 獲取某個單詞之前和之後的單詞嗎?
- 5. Python在某個單詞之前和之後找到n個單詞
- 6. 整個目錄和子目錄中搜索某個短語
- 7. 如何使用Hibernate Lucene搜索一個完整的短語和單個詞條?
- 8. 匹配某個單詞之前和之後的最多5個單詞
- 9. 如何在Java中搜索相似的單詞和短語?
- 10. 某個單詞之後的PHP修剪
- 11. 如何搜索Google Ngrams的「過期」單詞和短語?
- 12. Twitter API搜索使用的單詞和短語
- 13. 某個單詞之後提取數字
- 14. 在Mysql中多行搜索多個單詞和單個詞php
- 15. 搜索text_field中的每個單詞的搜索
- 16. 簡單搜索只搜索記錄的最後一個單詞
- 17. 在HTML中搜索和替換單詞
- 18. MongoDB全文搜索 - 匹配單詞和確切短語
- 19. 使用R TM包搜索2和3單詞短語
- 20. 需要一個工具來搜索大型結構文本文檔中的單詞,短語和相關短語
- 21. 在Flash Builder的「搜索」工具中搜索確切的單詞或短語?
- 22. 加粗字符串中每個單詞的每個實例
- 23. 在NSStrings數組中搜索完整的短語或單詞
- 24. 在Ruby中搜索單個單詞和組合單詞
- 25. 在一個單詞中搜索一個25 GB的語料庫
- 26. PHP:str_replace在一個單詞/短語
- 27. 正在搜索一個短語
- 28. MongoDB文本搜索和詞搜索的多個搜索詞
- 29. 替換單詞在搜索詞之前
- 30. iPhone - 搜索一個詞語詞典
這正是我一直在尋找,謝謝! – Zuse