Skip to content Skip to sidebar Skip to footer

Find All Words Containing Specific Letters And Special Characters

Suppose I have a list formed by words including special characters: one three# $four etc. I want to find all words in the list that contain specific letter

Solution 1:

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(','));
});

Is this what you want?

Solution 2:

If I understand correctly, all you need is to escape all special characters from your query string so that they are not considered as such by the RegEx engine. Something like this should work:

var myList = "<one></one> $two three#";
var query = "$two";
var myRegex = newRegEx(query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
alert(myList.match(myRegex));

Hat tip to the answer that provided the escaping mechanism.

Is this what you needed?

PD: I'd also recommend using console instead of alert.

Post a Comment for "Find All Words Containing Specific Letters And Special Characters"