Introduction
In this article, we will learn how to search and locate the words or substrings in paragraphs or large strings. We will achieve this by using both, JavaScript and jQuery.
Scenarios
Sometimes, we come across a requirement where we need to find the substrings in a paragraph. We have a couple of scenarios which we are going to look into to achieve it.
- How to find if a word or a substring is present in the given string.
- How to find a position of a particular word or a string in the given string
- How to find a string and replace it with some other string.
- How to find the number of occurrences of a particular word or a string in a given string or paragraph
- How to find the single or multiple paragraphs which have the required word or string.
How to find if a word or a substring is present in the given string?
In this case, we will use the includes() method which determines whether a string contains the specified word or a substring. If the word or substring is present in the given string, the includes() method returns true; otherwise, it returns false.
The includes() method is case-sensitive.
This method does search the string. Also, it has got the flexibility to identify the starting position of the search.
Note: includes() method is not supported in IE11
Code
Search the entire string.
<script language="javascript">
var strng;
strng = "Either way, time largely defines the human experience.";
var incStr = strng.includes("human");
document.write("Is 'human' present in the string? : " + incStr);
</script>
Output

Search for the specified start position.
Case 1 - Starting Point of the search is before ‘h’ in the word “human”
<script language="javascript">
var strng;
strng = 'Either way, time largely defines the human experience.';
var incStr = strng.includes('human');
document.write("Is 'human' present in the string? : " + incStr);
</script>
Output

Case 2 - Starting Point of search is after ‘h’ in the word “human”
<script language="javascript">
var strng;
strng = "Either way, time largely defines the human experience.";
var incStr = strng.includes("human", 38);
document.write("Is 'human' present in the string? : " + incStr);
</script>
Output

How to find a position of a particular word or a substring in the given string?
We can achieve this by using a Regular Expression which provides us the text search functionality with its string method: search(). This uses an expression to search for a match and returns the starting position of the desired word or substring. We can use the search() method with either a regular expression or string as an argument.
Code
- String as an argument.
<script language="javascript"> var strng; strng = "Is time an illusion created within our minds? Or is it a dimension of the physical universe? Either way,time largely defines the human experience."; var position = strng.search("illusion"); document.write("Desired String is present at position : " + position); </script> - Regular Expression as an argument.
<script language="javascript"> var strng; strng = "Is time an illusion created within our minds? Or is it a dimension of the physical universe? Either way,time largely defines the human experience."; var position = strng.search(/illusion/i); document.write("Desired String is present at position : " + position); </script>





Sagar PardeshiPosted May 31, 2020, 9:20 AM
Good article...keep it up..
Vishal PrajapatiPosted Feb 14, 2018, 4:33 AM
Awesome...thanks for sharing with us....JavaScript Lover..:)
Sanjay SharmaPosted Feb 13, 2018, 10:55 PM
Nice some more interesting facts here https://dev.to/adroitcoder/includes-vs-indexof-in-javascript.