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

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

如何從字符串中刪除除特殊類之外的所有 HTML 元素?

如何從字符串中刪除除特殊類之外的所有 HTML 元素?

呼喚遠方 2022-05-26 11:14:53
我有問題。我目前正在尋找一種從字符串中刪除任何 HTML 元素的方法。但是有兩個條件:應保留元素的內容不應刪除具有已定義類的特殊元素我已經嘗試了很多事情并查看了很多關于 SO 的問題/答案,但不幸的是我無法真正找出任何答案。不幸的是,這遠遠超出了我的能力。但我想知道這樣的事情是如何工作的。因此,當我有例如這樣的字符串時:You have to pay <div class="keep-this">$200</div> per <span class="date">month</span> for your <span class="vehicle">car</span>剝離后應該是這樣的:You have to pay <div class="keep-this">$200</div> per month for your car我實際上嘗試過以下事情:jQuery(document).ready(function ($) {  let string = 'You have to pay <div class="keep-this">$200</div> per <span class="date">month</span> for your <span class="vehicle">car</span>';  console.log(string);  function removeHTMLfromString(string) {    let tmp = document.createElement("DIV");    tmp.innerHTML = string;    return tmp.textContent || tmp.innerText || "";  }  console.log(removeHTMLfromString(string));  console.log(string.replace(/<[^>]*>?/gm, ''));});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>而且我還嘗試了一個正則表達式工具來查看刪除了什么,但不幸的是,我在這里也沒有取得太大進展:https://www.regexr.com/50qar如果有人可以幫助我完成這項任務,我會很高興。非常感謝!更新也許有一種方法只用一個正則表達式?如果是,在使用此正則表達式時,如何使用特殊類排除我的元素:/<\/?[^>]+(>|$)/g?
查看完整描述

2 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

它可能是一個有點大的代碼。但我認為它可能會對你有所幫助。


let str = 'You have to pay <div class="keep-this">$200</div> per <span class="date">month</span> for your <span class="vehicle">car</span> <div class="keep-this">$500</div> also';


const el = document.createElement("div");

el.innerHTML = str;


// Get all the elements to keep

const keep = el.querySelectorAll(".keep-this");


// Replace the keeping element from the original string

// With special pattern and index so that we can replace

// the pattern with original keeping element

keep.forEach((v, i) => {

  const keepStr = v.outerHTML;

  str = str.replace(keepStr, `_k${i}_`);

});


// Replace created element's innerHTML by patternised string.

el.innerHTML = str;


// Get the text only

let stringify = el.innerText;


// Replace patterns from the text string by keeping element

keep.forEach((v,i) => {

  const keepStr = v.outerHTML;

  stringify = stringify.replace(`_k${i}_`, keepStr);

});


console.log(stringify);

如果有任何誤導,請給我評論。

更新:正則表達式方法

使用正則表達式可以完成相同的任務。方法是——

  1. 通過正則表達式找到所有可保留的元素并存儲它們。

  2. 用相同的模式替換輸入字符串中的所有可保留元素

  3. 從 sting 中刪除所有 HTML 標記。

  4. 用可保留的元素替換相同的模式。

let htmlString = 'You have to pay <div class="keep-this">$200</div> per <span class="date">month</span> for your <span class="vehicle">car</span> Another <div class="keep-this">$400</div> here';


// RegExp for keep elements

const keepRegex = /<([a-z1-6]+)\s+(class=[\'\"](keep-this\s*.*?)[\'\"])[^>]*>.*?<\/\1>/ig;


// RegExp for opening tag

const openRegex = /<([a-z1-6]+)\b[^>]*>/ig;


// RegExp for closing tag

const closeRegex = /<\/[a-z1-6]+>/ig;


// Find all the matches for the keeping elements

const matches = [...htmlString.matchAll(keepRegex)];


// Replace the input string with any pattern so that it could be replaced later

matches.forEach((match, i) => {

  htmlString = htmlString.replace(match[0], `_k${i}_`);

});


// Remove opening tags from the input string

htmlString = htmlString.replace(openRegex, '');


// Remove closing tags from the input string

htmlString = htmlString.replace(closeRegex, '');


// Replace the previously created pattern by keeping element

matches.forEach((match, index) => {

  htmlString = htmlString.replace(`_k${index}_`, match[0]);

})


console.log(htmlString);


查看完整回答
反對 回復 2022-05-26
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

如果日期和車輛 div 和類來自另一個函數,你應該從那里擺脫它。



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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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