javascript search for words in sentence examples

Solutions on MaxInterview for javascript search for words in sentence examples by the best coders in the world

showing results for - "javascript search for words in sentence examples"
Alejandra
07 Sep 2020
1var myString = document.getElementById("str").innerHTML;
2var myWord = document.getElementById("txt1").value;
3var myPattern = new RegExp('(\\w*'+myWord+'\\w*)','gi');
4
5var matches = myString.match(myPattern);
6
7if (matches === null)
8{
9    document.getElementById("demo").innerHTML = "No results"; // Any message or empty
10    return;
11}
12
13document.getElementById("demo").innerHTML = matches + " - " +  matches.length + " result(s) found.";
14