I wanted to split a string and get the string in two parts.
for example:
www.medicoshere.com/register.html?23457cedlske234cd
i wish to split the string in the above url and store the string that is after the ? to a variable. How can i do that.
I wanted to split a string and get the string in two parts.
for example:
www.medicoshere.com/register.html?23457cedlske234cd
i wish to split the string in the above url and store the string that is after the ? to a variable. How can i do that.
var str = "www.medicoshere.com/register.html?23457cedlske234cd";
var res = str.split("?");
var value = res.slice(-1).pop(); // it will give 23457cedlske234cd
www.medicoshere.com?register.html?23457cedlske234cd then OP need to read index 2 then. Am I right ? It's just an example my concern was if there might be 2 ? then indexing will differ.?? Then it would be ? followed by additional parameters as &.You can get the part of the string you want in different ways
var str = "www.medicoshere.com/register.html?23457cedlske234cd";
var partIndex = str.indexOf("?");
var part = str.substring(partIndex + 1); // 23457cedlske234cd
var part = str.split("?")[1];
The only problem is that, if you use substring, part will contain the whole string if the part you tried finding is not part of the string, say
var str = "www.medicoshere.com/register.html#23457cedlske234cd";
var partIndex = str.indexOf("?");
var part = str.substring(partIndex + 1); // www.medicoshere.com/register.html#23457cedlske234cd
while using split would return undefined as the value of part.
Try:-
$scope.string = "www.medicoshere.com/register.html?23457cedlske234cd";
$scope.s = $scope.string.split("?")
console.log(s[1] );