简体   繁体   中英

javascript remove html tags without regex

I have a function that must remove html tags in a string, here's my code:

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);

Here's my input: <html><head><title>sample site</title></head><body><div>text<div>more text</div>and more...</div>in body</body></html>

But I get this output:

text
more text
and more...
in body

I'm pretty new to javascript, can someone explain why 'samle site' isn't in the output ?

Possibly because it is not there to begin with. Check jsConsole.read("#tb-first") first and see what's in it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM