DEV Community

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.


Add below code inside index.html

<!DOCTYPE html>
<html lang="en">
    <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="" rel="stylesheet">
    <script src=""></script>
    <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>
    <h1>Find the word in paragraph</h1>

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

      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 {
        $('#keyword').on('keyup', function(){
Enter fullscreen mode Exit fullscreen mode


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)

Editor guide
oliverleitner profile image
Oliver Leitner

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

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.

stackfindover profile image
varghesejose2020 profile image
Varghese Jose