javascript - Getting dynamic value from URL -
i tried multiple ways splits, substrings value of employeeid below, there effective regex way?
/api/getvalue;id=12345;age:25;employeeid=4?test=true&othervalues=test
i want employee id value 4
any appreciated
you use simple regular expression match on employeeid
:
// extract employeeid regex var employeeid = url.match(/;employeeid=(\d+)/, url)[1]; console.log(employeeid); >>> 4
or if you'd function value can selected use following:
function getvalue(url, name) { var m = new regexp(';' + name + '[=:](\\d+)', 'g').exec(url); if (m) { return m[1]; } return ''; } var age = getvalue( '/api/getvalue;id=12345;age:25;employeeid=4?test=true&othervalues=test', 'age' ); console.log(age); >>> 25
Comments
Post a Comment