kfirba
kfirba

Reputation: 5441

Finding a substring in a string

Lets say that I have these strings:

My dog is young  

I have a _dog_ 

Dogs are fine

I want to get any string that has dog in it only. This means I want only the first row to return. The two others, contains marks like _ which I don't want to get, or it has an extra s at the end.

I've tried doing something like:

$('#side-categories a').each(function () {
    var href = $(this).attr("href");
    if (href.indexOf(table) >= 0) {
        $(this).css("color", "red");
        $(this).parents("ul").show().prev('.do').text('-');
    }
});

But it also returns the _dog_ and the Dogs

Suggestions?

Upvotes: 0

Views: 136

Answers (3)

Scott Mermelstein
Scott Mermelstein

Reputation: 15397

This is an ideal case for regular expressions!

In this case, you want to do this:

$('#side-categories a').each(function () {
    var href = $(this).attr("href");
    if(href.match(/\sdog\s/)){
        $(this).css("color", "red");
        $(this).parents("ul").show().prev('.do').text('-');
    }
});

Regular expressions are a good thing to learn. In this case, it's pretty basic. /\sdog\s/ is saying "a regular expression containing some whitespace, the lowercase string dog and some more whitespace." That includes your first line, while eliminating your second (due to the whitespace) and the third (do to the exact string dog followed by whitespace).

If you're using a variable (such as table?) to hold the string you're looking for, you need to construct the regular expression. The important lines then become:

var regex = new RegExp("\s" + table + "\s");
if(href.match(regex)){

Based on your comments, it looks like you want to see if the string starts with dog. If that's the case, you can use regex - you just want new RegExp("^" + table). But in that case, you actually could do better with a regular string function, such as if (href.indexOf(table) == 0).

In the case of looking for table=dog*, that's a fairly easy RegExp as well: new RegExp("table=" + table). This looks for table=dog to be anywhere in the string. Without regexp, you'd do similar to above, if (href.indexOf("table=" + table) != -1).

Upvotes: 6

Dropout
Dropout

Reputation: 13876

You can also achieve this by using regex, but you would have to explicitly define that the word that you're using in the matching pattern has a space before it and after it, which is kind of wierd.

The condition that you're looking for is the one which splits the setence to words, because otherwise _dog_ sentence would be returned too. So:

function checkForWord(word, sentence)
{
    wordArray = sentence.split(" ");
    for(i=0; i<wordArray.length; i++)
    {
        if(wordArray[i] == word)
        {
            return true;
        }
    }
    return false;
}

Usage:

alert(checkForWord("dog", "My dog is young"));

Upvotes: 1

codingrose
codingrose

Reputation: 15709

Try:

if(href.match(/ dog /g)){
    $(this).css("color", "red");
    $(this).parents("ul").show().prev('.do').text('-');
}

DEMO here.

Upvotes: 1

Related Questions