1

I got 2 variables;

value = 'com'; longString= "com-233-123-232-123";

I'd like to check if "value" is inside "longString". I tried using regex with test() but I fail, maybe you know better.

0

4 Answers 4

3

I think the indexOf(substr, [start]) is enough no need to regex.

indexOf(substr, [start]) 

Searches and (if found) returns the index number of the searched character or substring within the string. If not found, -1 is returned. "Start" is an optional argument specifying the position within string to begin the search. Default is 0.

http://www.javascriptkit.com/javatutors/string4.shtml

Sign up to request clarification or add additional context in comments.

Comments

2

Two ways

1st indexOf

if (longString.indexOf(value) != -1)
   // found
else
   // not found

2nd split

var value = 'com'; var longString= "com-233-123-232-123";
var split1=longString.split("-");
var i=0;
var found=0;
while (i<split1.length)
{
    if(split1[i]==value)
    {
        found=1;
        break;
    }
    i++;
}
if(found==1)
    //found
else
    //not found

Comments

2

Don't use regular expressions for this - if the value you're looking for can be interpreted as a regular expression itself then you'll have trouble. Just check for longString.indexOf(value) != -1.

Comments

2

What does jQuery has to do with this? This is a simple Javascript problem

if (longString.indexOf(value) != -1)
    // We found it
else
   // We didn't find it

1 Comment

hmm I dont know man, just asking jquery maybe because I prefer jquery solution, but anything that works is good.

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.