0

I have two string I want to check if substring of one variable into another variable below is the code snippets.

var val1 = "www.google.com , www.yahoo.com , www.msn.com, in.news.yahoo.com";
    var val2 = "in.news.yahoo.com/huffington-post-removes-sonia-gandhi-rich-list-103404344.html";
//if val1 does not contains val2 I want to show popup. I tried val1 .toLowerCase().indexOf(val2 .toLowerCase()) but it doesn't work it always return -1.

can any one suggest me how can I put if condition?

1
  • That's because you are checking whether val2 is a substring of val1, not any substring of val2 is a substring of val1. Commented Dec 3, 2013 at 16:16

4 Answers 4

1

Try this:

var val1 = "www.google.com,www.yahoo.com,www.msn.com,in.news.yahoo.com";
var val2 = "www.yahoo.com/huffington-post-removes-sonia-gandhi-rich-list-103404344.html";

function check(){
    var val1_arr = val1.split(',');
    for(var i = 0; i < val1_arr.length; i++){
       if(val2.indexOf(val1_arr[i]) != -1){
         // val2 contains val1
           return true;
       }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Would be better to remove the need for jQuery. Plus [].each won't work. It'll need to be $.each(var1_arr, function(...
Sorry, I have just checked this. Try it again!
In your code, if val2 contains multiple instances of val1, you're going to get multiple popups.
0

try this:

val1 = [ "www.google.com",
         "www.yahoo.com",
         "www.msn.com",
         "in.news.yahoo.com" ];

function showPopUpIfUrlIsInVal1(val2) {
    var b = false;

    for (var i = 0; i < val1.length; i++)
    {
        if (val2.indexOf(val1[i]) > -1)
        {
            b = true;
            break;
        }
    }

    if (b)
    {
         alert("test message");
    }
}

showPopUpIfUrlIsInVal1("http://www.google.com");

Comments

0

Ah, this is a fun one, here's a FP way of solving this problem:

var val1 = "www.google.com , www.yahoo.com , www.msn.com,in.news.yahoo.com";
var val2 = "in.news.yahoo.com/huffington-post-removes-sonia-gandhi-rich-list-103404344.html";

function arrayInString(needles, haystack, callback) {
    var result = needles.some(function(el) {
        return haystack.indexOf(el.trim()); // Trim is required to remove extra whitespace
    });
    if (result) callback();
}

arrayInString(val1.split(","), val2, function() {
    // Create your popup here
});

Comments

0

You can use Array.prototype.filter method:

jsFiddle

   //fixing val1 format    
   var val1 = "www.google.com , www.yahoo.com , www.msn.com , in.news.yahoo.com";

    var matched = val1.split(' , ').filter(function (v) {
        return ~val2.indexOf(v);
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.