0

I have many links:

var one = '/news/local/edit?user=marcus&owner=ffff';
var two = '/news/other/edit?user=josh&owner=ddd';
var three = '/news/local/edit?user=john';
var four = '/news/local/test/marcus/edit?owner=aaaa&user=ady';

How can I get from these links the value of user?

This should return:

one = 'marcus';
two = 'josh';
three = 'john';
four = 'ady';

The result can be in an array.

NotMe
  • 87,343
  • 27
  • 171
  • 245

8 Answers8

0

Or, more failsafe:

var str = '/news/local/edit?user=marcus&owner=ffff';
var one = str.split('user=')[1].split('&')[0];
Alex
  • 2,398
  • 1
  • 16
  • 30
0

Or by searching and not spliting.

var capture = one.substring(one.indexOf("user=")+5);
var one = capture.substring(0, capture.indexOf("&"));
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
0

Try this

one = one.replace(/.*?user\=([a-zA-Z0-9]*)\&*.*/g, '$1')
two = two.replace(/.*?user\=([a-zA-Z0-9]*)\&*.*/g, '$1')
three = three.replace(/.*?user\=([a-zA-Z0-9]*)\&*.*/g, '$1')
emotionull
  • 595
  • 2
  • 8
  • 24
0

An alternative could be:

'/asd/?user=andy&algo=mas'.replace(/^.+user=([^\&]+).+$/, '$1') // returns 'andy'

Andrés Torres
  • 747
  • 5
  • 16
0

You can use a regular expression to extract the query string parameters. Please refer the below question.

How can I get query string values in JavaScript?

sample fiddle

related code from the answer edited according to the context here:

function getParameterByName(name,url) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(url);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

var one = '/news/local/edit?user=marcus&owner=ffff';
var two = '/news/other/edit?user=josh&owner=ddd';
var three = '/news/local/edit?user=john';
var four = '/news/local/test/marcus/edit?user=ady&owner=aaaa';

alert(getParameterByName("user",one));
alert(getParameterByName("user",two));
alert(getParameterByName("user",three));
alert(getParameterByName("user",four)); 
Community
  • 1
  • 1
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
0

This parser may do it for you ;)

function parseUser(str){
  var firstPart = str.substr(str.indexOf('user=') + 5); // +5 because the length of 'user='
  if(str.indexOf('&') !== -1)
    return firstPart.substr(0, firstPart.indexOf('&'));
  else
    return firstPart;
}
seniorpreacher
  • 666
  • 2
  • 11
  • 31
0

You can use this Regular expression to capture the user:

var one = '/news/local/edit?user=marcus&owner=ffff';

var userOne = /user=([^&]+)/.exec(one)[1]
levi
  • 23,693
  • 18
  • 59
  • 73
0

from here https://stackoverflow.com/a/901144/1113766 with just a small modification to receive as parameter the string were you want to look for the value.

function getParameterByName(name, path) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(path);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

http://jsfiddle.net/tjdragon/T62T2/

Community
  • 1
  • 1
Juan
  • 4,910
  • 3
  • 37
  • 46