0

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?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
kfirba
  • 5,231
  • 14
  • 41
  • 70

3 Answers3

6

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).

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • 2
    While passing a string to `.match` is OK, passing an actual regular expression would be cleaner. – Felix Kling Dec 05 '13 at 08:03
  • What if I want to pass a parameter instead of `dog`? – kfirba Dec 05 '13 at 08:08
  • @ScottMermelstein Thanks! But for some reason, it doesn't work for me. I will explain a lil bit more. I'm looking for a substring in a string that looks something like: `/table.php?table=xxxname=yyy` then I'm trying to see if xxx has the regex that I'm looking for. I have the xxx value in a hidden element on the page, so it's not the problem. but I think your code is refering to whitespaces delimiter, but there are actuallu none. there may be this sign: `_` and may be nothing. – kfirba Dec 05 '13 at 08:15
  • 1
    @kfirba: See, and that's why it's good to provide a real example of your problem, not just "some demonstration". Now we wasted a lot of time on solutions which don't help. It looks like you want to check whether the query string of a URL contains a parameter with a certain value, is that correct? Does your search term have to match a value exactly or only partially? There are already exiting solutions for extracting the parameters and values of a query string. – Felix Kling Dec 05 '13 at 08:19
  • @kfirba This should really be a comment and update to the question instead of my answer. You're getting closer to defining exactly what you want. Does xxx have to be exactly the term you're looking for? I know `_dog_` and `dogs` aren't ok, what about `foodog` and `dogfoo` and `-dog`? – Scott Mermelstein Dec 05 '13 at 08:19
  • Actually, it looks like you want it to start with `dog`? – Scott Mermelstein Dec 05 '13 at 08:21
  • @ScottMermelstein Starting with the expression may actually solve the problem – kfirba Dec 05 '13 at 08:23
  • @kfirba Updated, but I do agree with @FelixKling's comment - we took a long way around to solve your problem. That simple line of `/table.php?table=xxxname=yyy` made your issue much more clear. – Scott Mermelstein Dec 05 '13 at 08:25
  • So, you always want to test whether the value of a specific parameter in the query string matches your input? Maybe you should first extract the value form the URL then: http://stackoverflow.com/q/901115/218196. – Felix Kling Dec 05 '13 at 08:26
  • @ScottMermelstein I'm sorry that I'm annoying you, but the string that I'm getting from the `href` var, doesn't start with what I want. What I want to 'scan' is the value of `table` within the link: `/table.php?table=SCAN_THIS&name=yyy` – kfirba Dec 05 '13 at 08:31
  • @kfirba Ok, here's my final answer. (It's way past my bedtime!) If you need any further help, I'll be on in a few hours, or someone else can jump in from here. – Scott Mermelstein Dec 05 '13 at 08:35
  • @ScottMermelstein Thanks scot! works like magic! Just for you to know, for some reason, the Regex method **doesn't** work. The second one without the Regex, works perfectly fine. Thanks! – kfirba Dec 05 '13 at 08:41
1

Try:

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

DEMO here.

codingrose
  • 15,563
  • 11
  • 39
  • 58
1

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"));
Dropout
  • 13,653
  • 10
  • 56
  • 109