gpt4 book ai didi

javascript - JavaScript 中不区分大小写的搜索

转载 作者:行者123 更新时间:2023-12-01 01:14:44 25 4
gpt4 key购买 nike

我有这段 JavaScript 代码,应该在特定的时间后刷新给定的网页,并尝试在每次刷新后查找某个单词。当找到这个词时,就会发出某种令人震惊的声音。这是代码:

javascript:
var myRegExp = prompt("the word");
timeout = prompt("the time in seconds");
current = location.href;
setTimeout('reload()', 1000 * timeout);
var audio = new Audio('http://soundbible.com/grab.php?id=2197&type=mp3');

function reload() {
var found = searchText();
if (!found) {
setTimeout('reload()', 1000 * timeout);
fr4me = '<frameset cols=\'*\'>\n<frame id="frame01" src=\'' + current + '\'/>';
fr4me += '</frameset>';
with(document) {
write(fr4me);
void(close())
};
}
}

function searchText() {
var f = document.getElementById("frame01");
if (f != null && f.contentDocument != null) {
var t = f.contentDocument.body.innerHTML;
var matchPos = t.search(myRegExp);
if (matchPos != -1) {
audio.play();

return true;
} else {
return false;
}
}
}

我的问题/请求是,如何使搜索单词大小写不敏感?

最佳答案

使用ignoreCase选项来自 MDN

The ignoreCase property indicates whether or not the "i" flag is used with the regular expression. ignoreCase is a read-only property of an individual regular expression instance.

var regex1 = new RegExp('foo');
var regex2 = new RegExp('foo', 'i');

console.log(regex1.test('Football'));
// expected output: false

console.log(regex2.ignoreCase);
// expected output: true

console.log(regex2.test('Football'));
// expected output: true

关于javascript - JavaScript 中不区分大小写的搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54875120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com