Answers for "javascript search for words in sentence examples"

0

javascript search for words in sentence examples

var myString = document.getElementById("str").innerHTML;
var myWord = document.getElementById("txt1").value;
var myPattern = new RegExp('(\\w*'+myWord+'\\w*)','gi');

var matches = myString.match(myPattern);

if (matches === null)
{
    document.getElementById("demo").innerHTML = "No results"; // Any message or empty
    return;
}

document.getElementById("demo").innerHTML = matches + " - " +  matches.length + " result(s) found.";
Posted by: Guest on October-03-2021

Code answers related to "javascript search for words in sentence examples"

Code answers related to "Javascript"

Browse Popular Code Answers by Language