在 JavaScript 中,可以使用以下方法检索关键字:
var str = "This is a sample string.";
var keyword = "sample";
var index = str.indexOf(keyword);
console.log(index); // 输出:10
var str = "This is a sample string.";
var keyword = "sample";
var hasKeyword = str.includes(keyword);
console.log(hasKeyword); // 输出:true
var str = "This is a sample string.";
var keyword = /sample/;
var index = str.search(keyword);
console.log(index); // 输出:10
var str = "This is a sample string.";
var keyword = /sample/;
var matches = str.match(keyword);
console.log(matches); // 输出:["sample"]
这些方法可以用于字符串中的关键字检索,你可以根据具体需求选择适合的方法。