DEV Community

loading...
Cover image for How to find a word in a paragraph

How to find a word in a paragraph

stackfindover profile image Stackfindover ・2 min read

Hello, guys in this tutorial we will create a simple text finder ( find a word in a paragraph ) using HTML CSS & JavaScript.

Step:1

Add below code inside index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Text Finder</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="style.css" />
    <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@500&display=swap" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  </head>
  <body>
    <div class="content">
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.      </p>
    </div>
    <h1>Find the word in paragraph</h1>

    <div class="form">
      <input type="text" id="keyword" class="form_control" placeholder="Search...">
    </div>

    <script>
      function textFind(keyword) {
        if(keyword) {
          var content = $("p").text();
          var searchText = new RegExp(keyword, "ig");
          var matches = content.match(searchText);       

          if(matches) {
            $("p").html(content.replace(searchText, function(match){
              return "<span class='highlight'>"+match+"</span>";
            }));
          }else {
            $('.highlight').removeClass('highlight');
          }
        }else{
          $('.highlight').removeClass('highlight');
        }
      }
      $(document).ready(function(){
        $('#keyword').on('keyup', function(){
          textFind($(this).val());
        })
      });
    </script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step:2

Then we need to add code for style.css which code I provide in the below screen.

* {
  padding: 0;
  margin: 0;
  outline: 0;
  font-family: 'IBM Plex Sans', sans-serif;
}
body {
    height: 100vh;
    width: 100vw; 
    overflow: hidden;
}
.content {
  width: 80%;
  margin: 50px auto;
}
h1 {
  text-align: center;
}
.form {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: auto;
  width: 100%;
  max-width: 320px;
  height: 50vh;
}
input#keyword {
  font-size: 18px;
  padding: 10px 20px;
  outline: 0;
  border: 1px solid #0f62fe;
  width: auto;
}
span.highlight {
  color: red;
  text-shadow: 0 1px 1px red;
}
Enter fullscreen mode Exit fullscreen mode

Find a word in a paragraph video Output:

Find a word in a paragraph video Output:

We will update soon :)

Discussion (3)

pic
Editor guide
Collapse
oliverleitner profile image
Oliver Leitner

you dont need jquery or any framework for just text highlighting.

tutsmake.com/highlight-search-resu...

if you want to go by classname, you can do that too... which makes no sense in your div, if you just got one div on that page, id will do just fine.

Collapse
stackfindover profile image
Collapse
varghesejose2020 profile image
Varghese Jose

Helpful