查找所有包含特定字母和特殊字符的单词 [英] Find all words containing specific letters and special characters

查看:275
本文介绍了查找所有包含特定字母和特殊字符的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含特殊字符的单词组成的列表:

Suppose I have a list formed by words including special characters:

one
<two></two>
three#
$four
etc.

我想在列表中找到所有包含特定字母的单词,
我尝试使用

I want to find all words in the list that contain specific letters,
I've tried to use

var myList = "<one></one> $two three#";
var myRegex = /\bMYWORD[^\b]*?\b/gi;
alert(myList.match(myRegex));

但这不适用于特殊字符.

But this does not work with special characters..

DEMO

很遗憾,我是javascript的新手,我不知道创建列表的最佳方法是什么
并分隔列表中的单词.

Unfortunately I'm new to javascript, and I don't know what is the best way to create the list
and to separe the words in the list..

推荐答案

因此,根据您的输入,这可以解决问题:

So based on your inputs, this does the trick:

var myList = "<one></one> $two three#";
var items = myList.split(' ');  

$('#myInput').on('input',function(){
    var matches = [];
    for(var i = 0; i < items.length; i++) {
        if(items[i].indexOf(this.value) > -1)
            matches.push(items[i]);
    }


    $('#myDiv').text(matches.join(','));
});

是您想要的吗?

这篇关于查找所有包含特定字母和特殊字符的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆