How to search a specific word/string in whole big string and get index location through javascript
M Farhan M Farhan @ programming 2020-06-02 21:08:02
clicks 964

Hy. I want to match a word in whole big sentence and i want to get the index where it match in that sentence .Please help me.

suggestion

2

solution

6

Suggestions
Solution
osama

Use str.search("you word") .It returns index value.


osama
If you want to get the exact word which you try to match.Use match function

But match function is typically used for regular exression so it maybe not a better technique.


osama
str.indexOf also return index of matching substring.

osama
OK If you donot know the word you are searching then to get last index you can use lastIndexOf() to get last index after you word.

osama
There is a function (str.includes) who tells wheather your word is there in sentence OR not.It will return true if word exist otherwise it return false.

osama
Test solutiom


M Farhan
author of post

M Farhan
m.farhan0235@gmail.com
message
Login now to make suggestion for this post!