Last segment of URL with JavaScript
You can also use the lastIndexOf() function to locate the last occurrence of the /
character in your URL, then the substring() function to return the substring starting from that location:
console.log(this.href.substring(this.href.lastIndexOf('/') + 1));
That way, you'll avoid creating an array containing all your URL segments, as split()
does.
Get last part of the url
OK, so it appears no one here attempted to try the solution here before posting.
First things first cheeks...
. This is a tricky ID to find (You have to escape the periods). The label
is also not part of the internal html where ID is cheeks...
, so we need to find the adjacent element and look the a
anchor tag you're looking for.
Here's the code:
$(document).ready(function() {
$('#Cheeks\\.\\.\\.').next('label').find('a').each(function(){
var lasturl = $(this).attr('href');
var urlsplit = lasturl.split("/");
console.log(urlsplit);
var finalvar = urlsplit.pop();
console.log('Adding class: ' + finalvar);
$(this).addClass(finalvar);
});
});
And here is a working jsfiddle with the solution.
Display the last part of URL javascript?
<script type="text/javascript">
var segment_str = window.location.pathname; // return segment1/segment2/segment3/segment4
var segment_array = segment_str.split( '/' );
var last_segment = segment_array.pop();
document.write(last_segment); // alerts segment4
</script>
JsFiddle: http://jsfiddle.net/HNMV3/1/
Javascript to grab last part of the document.URL?
You could simply use .split() on window.location.href
, and grab the last entry.
Example:
var lastPart = window.location.href.split("/").pop();
jQuery get last part of URL
var str="url";
str.split("/")[3]
you can use split
URL - Get last part in PHP
use following
<?php
$link = $_SERVER['PHP_SELF'];
$link_array = explode('/',$link);
echo $page = end($link_array);
?>
pcre. How to get last part of url
The preg_replace
only replaces what you have it find. In this case product-123.html
. So you're replacing /product-123.html
with product-123.html
and the https://example.com.ua/part1/part2/part3
remains untouched.
To replace everything and only keep the match you'd do
echo
preg_replace('#.*/([a-zA-Z0-9_-]+\.html)$#','$1','https://example.com.ua/part1/part2/part3/product-123.html');
you don't need a regex though to accomplish this task, and if you did it'd probably be cleaner to use preg_match
.
Here's a preg_match
approach:
preg_match('#[a-zA-Z0-9_-]+\.html$#', 'https://example.com.ua/part1/part2/part3/product-123.html', $match);
echo $match[0];
Demo: https://3v4l.org/4o9RM
Regex demo: https://regex101.com/r/6dytu0/2/
Angular Get last part of url
You can use ActivatedRoute interface, it contains the router state tree
within the angular app’s memory.
The parent
property represents the parent of the route in the router state tree,
while the snapshot
property is the current snapshot of the route and url
is an
observable of the URL segments and it matched by this route.
Step 1: Import ActivatedRoute
import { ActivatedRoute } from '@angular/router';
Step 2: Inject ActivatedRoute in constructor.
constructor(private route: ActivatedRoute) { }
Step 3: Call it in Oninit lifecycle hook
constructor(private route: ActivatedRoute){}
ngOnInit() {
this.route.parent.snapshot.url[2].path;
}
Related Topics
How to Change the State Correctly (Read-Only Error)
How to Toggle Class to a Div Element in Reactjs
How to Hide Blinking Cursor in Input Text
Disable F5 and Browser Refresh Using JavaScript
The Onclick Is Not Working on First Click
Jquery on Click Function Not Working With Bootstrap
How to Handle Multiple Submit Buttons in a Form Using Angular Js
Javascript Generate Unique Number Based on String
How to Disable a Message When the Password Value Is Empty
How to Fire an Event When V-Model Changes
Json.Stringify Without Quotes on Properties
Detect Click into Iframe Using JavaScript
Convert an Object With Multiple Values Per Key into Array of Objects in Js
Bootstrap Datepicker Appearing At Incorrect Location in a Modal
Typescript: How to Take Subset of Attributes from an Object
How to Display All Imagefrom Array in Reactjs
Jquery Validation Plugin - Typeerror: $(...).Validate Is Not a Function