javascript中不区分大小写的搜索

问题描述 投票:0回答:2

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

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

javascript case-insensitive
2个回答
2
投票

使用ignoreCase选项来自MDN

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

0
投票

var regExp = /the word/i

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

© www.soinside.com 2019 - 2024. All rights reserved.