亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript,如何正確找到文本中的模式并替換?

Javascript,如何正確找到文本中的模式并替換?

一只甜甜圈 2022-01-07 19:03:06
在以下 html 文本中:<p>Here are some users you mentioned:    <span class="mention-inserted">@johnsmith1</span>,    <span class="mention-inserted">@daisy_apple23</span>,    <span class="mention-inserted">@bob.erricson</span>.    //... some more text //</p>我如何找到所有的<span class="mention-inserted">@{user}</span>并將它們轉換為<a></a>帶有添加類的標簽,保持它們各自的位置而不刪除任何其他文本?所以<span class="mention-inserted">@johnsmith1</span>應該簡單地轉換為<a class="mention-inserted active-link">@johnsmith1</a>& 很快。這比我相信的更復雜嗎?還是我公然錯過了正則表達式的簡單使用?
查看完整描述

2 回答

?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

與其嘗試為非常規語言提出正則表達式,不如使用querySelectorAll().這將允許您選擇所有具有 class 的 span 元素mention-inserted。然后,您可以使用 循環訪問此集合,.forEach并使用將span元素更改為鏈接.outerHTML。


請參見下面的示例:


document.querySelectorAll('span.mention-inserted').forEach(elem => {

  elem.outerHTML = `<a class="${elem.classList.value} active-link">${elem.innerHTML}</a>`;

});

.active-link {

  color: blue;

  cursor: pointer;

}

<p>

  Here are some users you mentioned:

  <span class="mention-inserted foo">@johnsmith1</span>,

  <span class="mention-inserted">@daisy_apple23</span>,

  <span class="mention-inserted">@bob.erricson</span>

</p>

如果您的文本是 JS 中的字符串(而不是 HTML),您可以使用 aDOMParser代替,這將允許您使用上述方法:


const str = `<p>Here are some users you mentioned: <span class="mention-inserted">@johnsmith1</span>,<span class="mention-inserted">@daisy_apple23</span>,<span class="mention-inserted">@bob.erricson</span></p>`;

const parsed = new DOMParser().parseFromString(str, "text/html").body;

parsed.querySelectorAll('span.mention-inserted').forEach(elem => {

  elem.outerHTML = `<a class="${elem.classList.value} active-link">${elem.innerHTML}</a>`;

});


const result = parsed.innerHTML; // String output/result

document.body.appendChild(parsed); // HTMLHtmlElement object output

console.log(result);

.active-link {

  color: blue;

  cursor: pointer;

}


查看完整回答
反對 回復 2022-01-07
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

我嘗試使用正則表達式來突出顯示 html textNode 而不是更改 html 結構,但這很復雜。所以我使用 DOM API 來實現它。


function replaceSpanWithAnchor(htmlText) {

  const node = document.createElement('div')

  node.innerHTML = htmlText

  const spans = node.querySelectorAll('span')

  for (let span of spans) {

    let a = document.createElement('a')

    a.className = "mention-inserted active-link"

    a.textContent = span.textContent

    span.replaceWith(a)

  }

  return node.innerHTML

}


console.log(replaceSpanWithAnchor('<p>Here are some users you mentioned: <span class="mention-inserted">@johnsmith1</span>,<span class="mention-inserted">@daisy_apple23</span>,<span class="mention-inserted">@bob.erricson</span>.</p>'))

// <p>Here are some users you mentioned: <a class="mention-inserted active-link">@johnsmith1</a>,<a class="mention-inserted active-link">@daisy_apple23</a>,<a class="mention-inserted active-link">@bob.erricson</a>.</p>


查看完整回答
反對 回復 2022-01-07
  • 2 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號