繁体   English   中英

JavaScript中不区分大小写的搜索

[英]case insensitive search in javascript

我有此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; } } } 

我的问题/要求是,如何使搜索单词不区分大小写?

使用 MDN中 ignoreCase选项

ignoreCase属性指示正则表达式是否使用“ i”标志。 ignoreCase是单个正则表达式实例的只读属性。

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

var regExp = /the word/i

有关正则表达式的更多信息: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM