📜  javascript 在句子示例中搜索单词 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:13.845000             🧑  作者: Mango

代码示例1
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.";