3

I'd like to achieve that, if "clientpsseabq" string is contained in variable Var_words then equal true, else false. I just have no idea what method or function do I need to use?

var Var_words = "https://www.go.me/outputsearchs/clientpsseabq"

if ( Var_words contains string "`clientpsseabq`"){
   return true;
}else{
   return false;
}

if someone could help me how can I complete this task?

afuzzyllama
  • 6,453
  • 5
  • 45
  • 64
olo
  • 5,043
  • 14
  • 47
  • 80

4 Answers4

3

You can try this

if (Var_words.indexOf("clientpsseabq") >= 0)

or with care of case sensitivity

if (Var_words.toLowerCase().indexOf("clientpsseabq") >= 0)
{
   // your code
}
Sachin
  • 39,043
  • 7
  • 86
  • 102
3

Use the (native JavaScript) function String.indexOf():

if(Var_words.indexOf('clientpsseabq') !== -1) {
    return true;
} else {
    return false;
}

.indexOf() returns the index of the string. If the string is not found, it returns -1.

A smaller, cleaner solution would be to simply return the value of the conditional directly:

return (Var_words.indexOf('clientpsseabq') !== -1);
Bojangles
  • 96,145
  • 48
  • 166
  • 203
1
 if(Var_words.indexOf("clientpsseabq") >= 0))
 {

 }
andy
  • 5,802
  • 2
  • 25
  • 49
Mahesh Patidar
  • 184
  • 3
  • 15
1

use a regular expression to test for the case

if(/clientpsseabq/.test(Var_words)){
    //given string exists
} else {
    //given string does not exists
}
Arun P Johny
  • 376,738
  • 64
  • 519
  • 520