Tuesday 21 August 2018

How to find a string within a jQuery or javascript string

Sometimes, you required to find a the existence of a small string with in a string. This article will demonstarte, how could you do by using plain JavaScript. 
First Method 
  1. //returns -1 if doesn't contains 
  1. if (str.indexOf("Hello") >= 0)  
  1. { 
  1.  //string contains Hello 
  1. } 
  1. //OR 
  1. //For your are not sure about the case of the string 
  1. //returns -1 if doesn't contains 
  1. if (str.toLowerCase().indexOf("hello") >= 0) 
  1. { 
  1.  //string contains hello 
  1. } 
This indexOf() method will return the position of the match, or -1 if it isn’t found.  
Second Method 
  1. //returns false if doesn't contains 
  1. if (/hello/i.test(str)) 
  1. { 
  1.  //string contain Hello 
  1. } 

No comments:

Post a Comment

How to find a string within a jQuery or javascript string

Sometimes, you required to find a the existence of a small string with in a string. This article will  demonstarte , how could you do by...