33

I want to check is some text is in a string for instance i have a string

str = "car, bycicle, bus"

and I have another string

str2 = "car"

I want to check if str2 is in str.

I am a newbie in javascript so please bear with me :)

Regards

user710502
  • 11,181
  • 29
  • 106
  • 161
  • A nice reference on javascript `String` methods: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String – Diego Jul 07 '11 at 17:16

8 Answers8

40
if(str.indexOf(str2) >= 0) {
   ...
}

Or if you want to go the regex route:

if(new RegExp(str2).test(str)) {
  ...
}

However you may face issues with escaping (metacharacters) in the latter, so the first route is easier.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
10

ES5

if(str.indexOf(str2) >= 0) {
   ...
}

ES6

if (str.includes(str2)) {

}
Furkan Durmaz
  • 101
  • 1
  • 2
4

Use the builtin .includes() string method to check for the existence of sub-string.
It return boolean which indicates if the sub-string included or not.

const string = "hello world";
const subString = "world";

console.log(string.includes(subString));

if(string.includes(subString)){
   // SOME CODE
}
shahar_m
  • 3,461
  • 5
  • 41
  • 61
4

str.lastIndexOf(str2) >= 0; this should work. untested though.

let str = "car, bycicle, bus";
let str2 = "car";
console.log(str.lastIndexOf(str2) >= 0);
Karan
  • 12,059
  • 3
  • 24
  • 40
Doug Chamberlain
  • 11,192
  • 9
  • 51
  • 91
  • why use `lastIndexOf` instead of just `indexOf`? – hughes Jul 07 '11 at 17:14
  • Just force of habit, i've gotten burned in the past using indexOf...lastIndexOf works the same as far as I know when there is only one instance of the string being looked for. – Doug Chamberlain Jul 07 '11 at 17:16
  • 1
    Interesting. What circumstances would case `indexOf` to cause trouble? – hughes Jul 07 '11 at 17:17
  • If you are looking for more than one way to skin this cat you could `var items = str.split(",")` into an array then iterate through the array items checking for items[i] == str2.... – Doug Chamberlain Jul 07 '11 at 17:20
  • @Hughes, Not at all saying my way is right, and not something I've done in practice in a long time but, `var x = ',,'` `x.indexOf(x) == 0` `x.lastIndexOf(x) == 1`, you'd get two different results, and depending on the logic down the line you could have some issues. – Doug Chamberlain Jul 07 '11 at 17:24
2

Please use this :

var s = "foo";
alert(s.indexOf("oo") > -1);
JensG
  • 13,148
  • 4
  • 45
  • 55
  • How is this different from the accepted answer except that you use `>` instead of `>=`? – Ram Jul 22 '15 at 18:16
  • Ram, Both are the same!! If you will see conditions then both are indicating the same that whatever the value you get using "indexOf" function should be greater than or equal 0. – darshankhamar Jul 27 '15 at 11:07
  • 3
    Both are the same that's what I meant. Please don't add a duplicate answer to the question instead upvote the existing answer if desired. – Ram Jul 27 '15 at 16:58
0

You can use this:

'a nice string'.includes('nice')
EssaidiM
  • 7,924
  • 1
  • 18
  • 22
0

This function will tell you if the substring is in the string and how many times.

 const textInString = (wordToFind, wholeText) => {
    const text = new RegExp(wordToFind, 'g');
    const occurence = wholeText.match(text) ?? [];
    return [occurence.length > 0, occurence.length]
 }
 
 console.log(textInString("is", "This cow jumped over this moon"))
N Djel Okoye
  • 950
  • 12
  • 10
-1

If you just want to check substring in a string you can use indexOf but if you want to check if the word is in the string or not, the other answers might not work correctly for example:

str = "carpet, bycicle, bus"
str2 = "car"
What you want car word is found not car in carpet
if(str.indexOf(str2) >= 0) {
  // Still true here
}
// OR 
if(new RegExp(str2).test(str)) {
  // Still true here 
}

So you can improve the regex a bit to make it work

str = "carpet, bycicle, bus"
str1 = "car, bycicle, bus"
stringCheck = "car"
// This will false
if(new RegExp(`\b${stringCheck}\b`).test(str)) {
  
}
// This will true
if(new RegExp(`\b${stringCheck}\b`,"g").test(str1)) {
  
}
Tony Nguyen
  • 3,298
  • 11
  • 19