增量正则表达式过滤器
我有一长串 标签,我想实时过滤它们;即删除不匹配的。我会根据输入字段中的文本输入来过滤它们。正则表达式显然会搜索 a 标签内容以查找与输入字段的匹配,但我想知道如何使其更漂亮,并像谷歌的搜索栏现在那样主动过滤列表。我想,按键功能会触发正则表达式功能。
我不知道该怎么做的部分是:
[input field]ArI[/]
列表:
• ArIes
• ArIstotle
即如何让它检查列表项的第 n 个字母。
编辑
这是我到目前为止所拥有的,但它不起作用。
$("input.CardName_Input").keyup(function() {
var getPhrase = $(".CardName_Input").val();
new RegExp('^' + getPhrase + '.', 'i');
$("#Results a").each(function() {
if (!($(this).val().match(RegExp))) {
$(this).addClass("HIDE");
}
})
});
I have a long list of <a>
tags, and i'd like to filter through them live; that is, to remove the ones that don't match. I'd filter through them based on the text input in an input field. Regex would obviously search the a tag contents for a match to the input field, but i was wondering how to make it fancier, and actively filter the list like google's search bar does these days. I imagine, a key up function would trigger the regex function.
The part i don't know how to do is this:
[input field]ArI[/]
List:
• ArIes
• ArIstotle
i.e., how to make it check the nth letter of the list item.
EDIT
This is what i have so far, and it doesn't work.
$("input.CardName_Input").keyup(function() {
var getPhrase = $(".CardName_Input").val();
new RegExp('^' + getPhrase + '.', 'i');
$("#Results a").each(function() {
if (!($(this).val().match(RegExp))) {
$(this).addClass("HIDE");
}
})
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解,您只需检查每个列表项字符串的开头?
那么你可以使用类似
/^Ar./i
作为表达式吗?在您的情况下,您可以动态构建 RegExp
new RegExp('^' + searchString + '.', 'i')
As far as I understand you just have to check for the beginning of each list item string?
Then you could use something like
/^Ar./i
as expression?In your case you could build the RegExp dynamically
new RegExp('^' + searchString + '.', 'i')
该代码构造了一个
new RegExp
,但实际上并没有用它做任何事情。然后它将RegExp
(函数)传递给match
。您需要将正则表达式分配给一个变量,以便可以使用它,并将 that 传递给match
:NB 如果您不想要特殊的
.CardName_Input
中的字符要破坏您的代码,您需要 转义getPhrase
。The code constructs a
new RegExp
but doesn't actually do anything with it. Then it passesRegExp
(a function) tomatch
. You need to assign the regexp to a variable so you can use it, and pass that tomatch
:N.B. if you don't want special characters in the
.CardName_Input
to break your code, you'll need to escapegetPhrase
.