Vikash Vikash - 2 months ago 12
Javascript Question

How to search string between two character in jQuery

I want to search string between two characters in JavaScript, jQuery.

Here id my url


http://local.evibe-dash.in/vendors/new?status=Phone&count=60&type="artist,magic".


I want to search string between
"status=" and first &
, so that when I will get other value than this, then I can put in URL.

Answer

Using match() with capturing group regex

var str = 'http://local.evibe-dash.in/vendors/new?status=Phone&count=60&type="artist,m‌​agic".';

var res = str.match(/status=([^&]+)/)[1]

document.write(res);


or using split()

var str = 'http://local.evibe-dash.in/vendors/new?status=Phone&count=60&type="artist,m‌​agic".';

var res = str.split('status=')[1].split('&')[0];

document.write(res);


or using substring() and indexOf()

var str = 'http://local.evibe-dash.in/vendors/new?status=Phone&count=60&type="artist,m‌​agic".',
  ind = str.indexOf('status=');

var res = str.substring(ind + 7, str.indexOf('&', ind));

document.write(res);

Comments