简体   繁体   中英

how to remove HTML tags from a string in JavaScript without using regexp?

I am new to programming and I was solving this exercise. I have tried 3 loops with string.slice() but for some reason it prints an empty string.

Would you please explain what happens inside my code and why it prints the wrong output and how I can correct, rather than giving me your version of the correct answer, so that I can learn from my mistakes.

the test input is

<p><strong><em>PHP Exercises</em></strong></p>

and output should be PHP Exercises

ps this is not a PHP exercise, I'm not confused

here is my code :

function remove(answer){ 

    var sen = answer.split("");
    var arr = [];
    for (var i = 0; i<answer.length; i++){
        if (answer[i] == "<"){
            for (var j = i; j<answer.length; j++){
                if (answer[j] == ">"){
                    for (var k = j; k<answer.length; k++){
                        if (answer[k] == "<"){
                            return answer.slice(j+1, k);                
                        }
                    }
                }
            }
        }
    }
}

Try this:

 function stripTags(data) { var tmpElement = document.createElement("div"); tmpElement.innerHTML = data; return tmpElement.textContent || tmpElement.innerText || ""; } var something = '<p><strong><em>PHP Exercises</em></strong></p>'; alert(stripTags(something)); 

or You can use string.js ( string.js link ):

 var S = window.S; var something = '<p><strong><em>PHP Exercises</em></strong></p>'; something = S(something).stripTags().s; alert(something); 
 <script src="https://raw.githubusercontent.com/jprichardson/string.js/master/dist/string.min.js"></script> 

if You're trying nodejs so:

var S = require('string');
var something = '<p><strong><em>PHP Exercises</em></strong></p>';
something = S(something).stripTags().s;
console.log(something);

As to why the provided code isn't working, the code returns when j = 2 and k = 3. I discovered this by writing console.log(j, k); immediately before the return. This insight made it clear that the code is identifying the first set of open tags, when actually you seem to want to identify the open and closed "em" tags. The answers provided by others are more robust, but a quick fix to your code is:

change

if (answer[i] == "<"){

to

if (answer.slice(i, i+3) == "<em"){

Hope this helps!

Your code does not account for ... nothing . It simply stops at the first encounter of what's between ">" and "<", which is, in the first case, is nothing! You should check if a character is present, and move on if not.

Honestly, this is one of those useless exercises that text books use to try to get you to think outside the box. But you will never want to loop through a string to find text between tags. There are so many methods built in to JavaScript, it's literally reinventing the wheel to do this... that is if a wheel were really a for-loop.

If you really want to avoid Regex and other built in functions so that you can learn to problem solve the long way, well try slicing by brackets 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