Bilal Ahmed Janjua
Bilal Ahmed Janjua

Reputation: 81

Get specific text from a string java script

I m working with strings in java script, for e.g if i have the below string:

str = "miniprofile-container http://www.linkedin.com/miniprofile?vieweeID=8573&context=anet&view"

i want the ID number "8573" from this type of strings. They are of different length but have the same struct as the above. So kindly help me in achieving it.

I may use the strcpy function, but it will not be a generic approach

Upvotes: 0

Views: 1775

Answers (4)

adeneo
adeneo

Reputation: 318342

str.match(/vieweeID\=(.*?)\&/)[1]

FIDDLE

Upvotes: 1

palaѕн
palaѕн

Reputation: 73966

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

Upvotes: 1

DVM
DVM

Reputation: 1238

You can also try this if you don't want/need to use regex:

var str = "miniprofile-container http://www.linkedin.com/miniprofile?vieweeID=8573&context=anet&view";
    var s1 = str.indexOf("vieweeID=")+"vieweeID=".length;
    var matches = str.substring(s1,str.indexOf("&context"))

alert(matches);

http://jsfiddle.net/Qex8J/1/

Upvotes: 1

Eli
Eli

Reputation: 14827

var str = "miniprofile-container http://www.linkedin.com/miniprofile?vieweeID=8573&context=anet&view";
var pattern = /[0-9]+/g;
var matches = str.match(pattern); //8573

Demo: http://jsfiddle.net/Qex8J/

Upvotes: 1

Related Questions