JavaScript的关键preSS功能:不区分大小写AZ,数字和一些特殊字符?大小写、特殊字符、关键、功能

2023-09-10 17:59:27 作者:古城旧人

$('.s').keyup(function(e) {

    if (!/[A-Za-z0-9]/.test(String.fromCharCode(e.which))) {
        return false;
    }

我不知道什么是我的应用的最佳解决方案,正则表达式。 我有一个基于AJAX的搜索应该只是触发搜索时,实际的字符pssed像AZ(大写和小写),$ P $,数字,也许一个问号,破折号(连字符),和一个惊叹号。还空格应启用

I wonder what is the best regex solution for my application. I have an ajax-based search that should just trigger the search when actual characters are pressed like a-Z (upper and lowercase), numbers and maybe a questionmark, a dash(hyphen), and an exclamation mark. Also the spacebar should be enabled.

否则,AJAX搜索将被触发,以及如果SHIFT-,选项,或控制键,为pressed。

Otherwise the ajax search would be triggered as well if the shift-, option, or control-key, is pressed.

什么是最简单的正则表达式来这里明白了吗? 感谢您的帮助

What's the easiest regex pattern to understand here? thank you for your help

推荐答案

你的正则表达式的基本形式似乎很好,只是有你想要的东西,包括和注意重音字符。或者,您可能的不包括的你的的字符不的希望。

The basic form of your regex seems fine, just include what you want to include, and be aware of accented characters. Alternately, you might exclude the characters you don't want.

不过,我会使用键preSS 事件,而不是 KEYUP 事件这一点。 键preSS 的按键重复闪光时的实际typeable字符输入,和火灾(而你只有一次 KEYUP 即使键重复)。 键preSS 不解雇移位,元,按Ctrl等 更新:这不是不一定真正的跨浏览器的(叹气)的,请参阅下面的更新。

But I'd use the keypress event rather than keyup event for this. keypress fires when an actual typeable character is typed, and fires on key repeat (whereas you only get one keyup even if a key repeats). keypress is not fired for Shift, Meta, Ctrl, etc. Update: This isn't necessarily true cross-browser (sigh), see the update below.

如果你钩住键preSS ,我可能不会过滤出什么(不需要正则表达式),因为我而推迟到浏览器和语言环境的意识什么consitutes一个真正的角色。我也很可能有一个短暂的延迟,以避免大量不必要的搜索。如果这个人相当快的类型弗雷德有没有必要搜索F,FR和FRE。

If you're hooking keypress, I probably wouldn't filter anything out (no need for the regex), because I'd rather defer to the browser and its awareness of locale for what consitutes a real character. I'd also probably include a brief delay so as to avoid lots of unnecessary searches. If the person fairly rapidly types "fred" there's no need to search on "f", "fr", and "fre".

这里是我的意思(合并键preSS 有轻微的延迟)的例子。如果你真的想过滤掉某些字符,你可以做到这一点,在事件处理程序,但我有不低于上述原因:

Here's an example of what I mean (combining keypress with a slight delay). If you really want to filter out certain chars, you can do that in the event handler, but I haven't below for the reasons above:

HTML:

<input type='text' id='theText'>

的JavaScript(使用jQuery,因为你标记你的问题的jQuery ):

jQuery(function($) {
  var searchTimer = 0;

  $('#theText').keypress(function() {
    if (searchTimer != 0) {
      clearTimeout(searchTimer);
    }
    searchTimer = setTimeout(doSearch, 250);
  });

  function doSearch() {
    searchTimer = 0;
    display("Search: '" + $('#theText').val() + "'");
  }

  function display(msg) {
    $('<p/>').html(msg).appendTo(document.body);
  }
});​

活拷贝

这个例子搜索四分之一秒后,最后键preSS 它看到;参数调整到的setTimeout ,你认为合适。

That example searches a quarter second after the last keypress it sees; adjust the parameter to setTimeout as you see fit.

更新:在低于约箭头键您的评论,我想:但你没有看到键preSS箭头键 , 你做?得到的答复是:你做歌剧和其他几个。 叹息的所以,是的,有一个过滤器:

Update: After your comment below about arrow keys, I thought "but you don't see arrow keys on keypress, do you?" and the answer is: You do on Opera and a couple of others. sigh So yes, with a filter:

$('#theText').keypress(function(event) {
  // Magic numbers from http://unixpapa.com/js/key.html
  switch (event.keyCode) {
    case 37: // Left
    case 38: // Up
    case 39: // Right
    case 40: // Down
      break;

    default:
      // A "real" key, include it
      if (searchTimer != 0) {
        clearTimeout(searchTimer);
      }
      searchTimer = setTimeout(doSearch, 250);
  }
});

function doSearch() {
  searchTimer = 0;
  display("Search: '" + $('#theText').val() + "'");
}

活拷贝的(这个只是筛选出的箭头键,你会想扩展的)的

如果你还不知道吧,此页面是伟大的各地信息疯狂是跨浏览器的键盘事件。这可能是有点过时。

If you don't already know if it, this page is great for information around the madness that is keyboard events across browsers. It may be slightly dated.