Can you take out the word "null" and just type on the screen "word not found" ? (using match)

Asked

Viewed 59 times

-3

That’s what I tried to do, but keeps showing up:

class 46 of the javascript course

null

Word not found

<!DOCTYPE html>
<html lang="pt-br">
<head>
        <meta charset="utf-8">
        <title> Match - Procura elemento </title> 
        <script>
    
        function carrega(){
           var texto = document.getElementById("txt").innerHTML;
           var res = texto.match("html");
           document.write(texto+"</br></br>");
           document.write(res+"</br>");
           
           
           if(res == null){
             document.write("Palavra não encontrada");
           }else{
            document.write("Encontrou: "+res.length+" Palavra");
            }
           }
           window.addEventListener("load",carrega);
           
           
        </script>
</head>
<body>
      <p id="txt">aula 46 do curso de javascript</p>
</body>
</html>

The Result has to be :

class 46 of the javascript course

Word not found

2 answers

0

Just make sure res within a decision structure, example:

if (res) document.write(res + "</br>");

Complete code:

function carrega() {
  var texto = document.getElementById("txt").innerHTML;
  var res = texto.match("html");
  document.write(texto + "</br></br>");
  if (res) document.write(res + "</br>");


  if (res == null) {
    document.write("Palavra não encontrada");
  } else {
    document.write("Encontrou: " + res.length + " Palavra");
  }
}
window.addEventListener("load", carrega);
<p id="txt">aula 46 do curso de javascript</p>

in null is false in the decision structure (this is characteristic of the language itself), so it was checked in the simplest way, but, can also put so:

if (res !== null)

that has the same result as the other structure. You really have to be careful with Javascript if you are sure the return can be done so, if you do not have to do other checks.

References

  • Thank you, gave the result I wanted.

  • But I just don’t quite understand what that means : if (res) Document.write(res + "</br>");

  • Dei um complementada @Dereguepinto is a feature of Javascript this type of comparison in results that may come null.

0

I kept trying here , and it worked that way :

<!DOCTYPE html>
<html lang="pt-br">
<head>
        <meta charset="utf-8">
        <title> Match - Procura elemento </title> 
        <script>
    
        function carrega(){
           var texto = document.getElementById("txt").innerHTML;
           var res = texto.match("curso");
           document.write(texto+"</br></br>");
           
           if(res == null){
             document.write("Palavra não encontrada");
           }else{
            document.write(res+"</br>");
            document.write("Encontrou: "+res.length+" Palavra");
            }
           }
           window.addEventListener("load",carrega);
           
        </script>
</head>
<body>
      <p id="txt">aula 46 do curso de javascript</p>
</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.