Get url after slash javascript
Exploding an url in javascript can be done using the official rfc2396 regex: Show
This will gives you:
Where you can, in your case, easily retrieve you path with:
And therefore the first word after the path using: JavaScript can access the current URL in parts. For this URL: So to get the full URL path in JavaScript: A bit of a more modern way of working with URLs is the URL() global method. If
you need to break up up the pathname, for example, a URL like https://css-tricks.com/blah/blah/blah/index.html, you can split the string on “/” characters Then access the different parts by the parts of the array, like To put that pathname back together, you can stitch together the array and put the “/”‘s back in: Probably the quickest way to take a peak at what you have is to put To split a URL, use the split() method. Use toString() before that. Let us see an example Examplevar newURL="http://www.example.com/index.html/homePage/aboutus/"; console.log(newURL); var splitURL=newURL.toString().split("/"); console.log(splitURL); Above, we have set forward slash in the split() function, since we need to split the URL after every such slash. To run the above program, you need to use the following command − node fileName.js. OutputHere, my file name is demo150.js. This will produce the following output − PS C:\Users\Amit\JavaScript-code> node demo150.js http://www.example.com/index.html/homePage/aboutus/[ 'http:', '', 'www.example.com', 'index.html', 'homePage', 'aboutus', '' ]
Updated on 11-Sep-2020 08:36:01
View Discussion Improve Article Save Article View Discussion Improve Article Save Article The task is to get the string after a specific character(‘/’). Here are few of the mostly used techniques discussed. We are going to use
JavaScript.
Example 1: This example using the approach discussed above.
Output: Approach 2:
Example 2: This example using the approach discussed above.
Output: How do I get the value of a URL after a slash?Use the . substring() method to get the access the string after last slash.
How to get URL path in JavaScript?window.location.href returns the href (URL) of the current page. window.location.hostname returns the domain name of the web host. window.location.pathname returns the path and filename of the current page.
How to split URL by slash JavaScript?“javascript split url last slash” Code Answer's. var str = "var1/var2/var3";. var rest = str. substring(0, str. lastIndexOf("/") + 1);. var last = str. substring(str. lastIndexOf("/") + 1, str. length);. console. log(rest);. console. log(last);. How do I get the current URL?Answer: Use the window. location. href Property
location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.
|