0

I have url "SampleProject/profile/aA12". How can I get the value of the id from my rewritten URL using javascript? I want to get the "aA12" value.

Im using htaccess rewrite to rewrite my URL. Im new in rewritting url's. Any help will be appreciated. More powers and thank you.

3 Answers 3

1

You can use regex.

Try

'SampleProject/profile/aA12'.match(/\SampleProject\/profile\/(\w+)/)

'SampleProject/profile/aA12/xxx'.match(/\SampleProject\/profile\/(\w+)/)

'aA12' will be matched in both cases.

Sign up to request clarification or add additional context in comments.

Comments

0

There are going to be quite a few ways to achieve your goal with JavaScript. A simple solution could be something like this:

let myURL = "SampleProject/profile/aA12";

let result = myURL.split('/').pop();
// returns "aA12"

The .split('/') method is dividing your string up into an array using the / character, and .pop() is simply returning the last element of that array.

Hope this helps! If you were looking for more advanced matching, i.e. if you wanted to ignore a potential query string on the end of the URL parameter, you could use regular expressions.

1 Comment

In my previous work im using this function "function getQueryStringValue(key) { return decodeURIComponent(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURIComponent(key).replace(/[\.\+*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1")); }" I dont know where to edit this to achieve my goal.
0

Their is a many way that you can use to achieve the desired method i made you a code pen in this link

var url = "SampleProject/profile/aA12";

let res = url.split('/').pop();
console.log(res)

https://codepen.io/anon/pen/KQxNja

1 Comment

how to get the current url using javascript?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.