Browsing "Older Posts"

Use indexOf() method of JavaScript string to  check if string contains particular given sub string or not.  String.prototype.indexOf returns the position of the string in the other string. If not found, it will return -1:

var string = "some string",
    substring = "some";
if(string.indexOf(substring) > -1) {
   // String contains given substring
   // do your stuff here
}else {
  // String haven't containg given substing
}

JavaScript - Check if string contains given substring

By yash → Wednesday, May 11, 2016
Problem:

Many of the time we require to get particular portion of the string for further modification. At this time we need to split particular sting.

Solution:

In JavaScript  we can use split() method with regular expression to split  sting and get desired results. Let's take a look at following given string.

first second third fourth fifth     sixth            Seventh 

"\s" is a regular expression which matches spaces, tabs and new line etc. "\s+" regular expression will match multiple occurrence of the white space and tabs.

var input = "first second third fourth fifth     sixth            Seventh ";
var output = input.split(/\s+/);
console.log(output);

Output: 

Output will be a array and it will look something like following image.


JavaScript - Spilit string by single or multiple white space

By yash →

Javascript:



var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

if(is_chrome){  
//You are browsing on chrome browser"
}else {
//You are browsing on other than chrome browser;
}
Tags:

Detect google chrome using javascript

By yash → Friday, May 6, 2016