簡體   English   中英

javascript刪除沒有正則表達式的html標簽

[英]javascript remove html tags without regex

我有一個必須刪除字符串中的html標簽的函數,這是我的代碼:

var text = jsConsole.read("#tb-first");
for (var i = 0; i < text.length; i++) {
  if (text[i] === '<') {
    do {
      text.replace(text[i], '');
      i++;
    } while (text[i] !== '>');
    text.replace(text[i], '');
  }
}
jsConsole.writeLine(text);

這是我的輸入: <html><head><title>sample site</title></head><body><div>text<div>more text</div>and more...</div>in body</body></html>

但是我得到以下輸出:

text
more text
and more...
in body

我對javascript很陌生,有人可以解釋為什么輸出中沒有'samle site'嗎?

可能因為它不存在。 首先檢查jsConsole.read("#tb-first")並查看其中的內容。

console.log(jsConsole.read("#tb-first"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM