How to search string in JavaScript





Lets say you have a string "Quick brown fox jumped over the lazy dog" and you want to check if the string has "do" in it.
Well its very simple! What you have to use is  JavaScript's built in  String.includes() method. More details on the method is here.

Eg : one

let x = "Quick brown fox jumped over the lazy dog"
x.includes("do") //true



Eg: Two, 
let x = [
  "Quick",
  "lazy",
  "Apple and banana",
  "fish soup",
];

 // other methods can also be used in the below example (string array one)
let is_a = x.filter((item)=>item.includes("a")) // [ 'lazy', 'Apple and banana' ]



Eg: 3

let x = [
  {
    name: "quick",
    value: "lazy",
  },
  {
    name: "apple ",
    value: "banana",
  },
  {
    name: "fish",
    value: "soup",
  },
  {
    name: "tomato",
    value: "carrot",
  },
];

let a = x.filter((item) => item.name.includes("a"));
// [
//   { name: "apple ", value: "banana" },
//   { name: "tomato", value: "carrot" },
// ];



That's it for now!


Comments