简体   繁体   中英

case insensitive search in javascript

I have this javascript code that is supposed to be refreshing a given web page after a specific amount of time and tries to find a certain word after each refresh. And when that word is found, a certain alarming sound is supposed to go off. here's the code:

 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; } } } 

My question/request is, how to make the search for the word case insensitive?

Use the ignoreCase option From the 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

var regExp = /the word/i

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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