Replace last forward slash in string with other character
javascript, jquery, regex, replace
Solution
Try this:
var str = "http://one/two/three/four";
console.log(str.replace(/\/(?=[^\/]*$)/, '#'));
That says: "replace the slash that is followed by either nothing or characters that do not include another slash, right to the end."
Problem
I'm trying to replace the last forward slash in a series of url strings, to exchange that forward slash for a number sign, like so - ``` http://example.com/about/our-company ``` to ``` http://example.com/about#our-company ``` Using JS with jQuery I've tried applying the replace function on each string but not sure as how to target just the last forward slash. ``` $('.submenu a').each(function() { var url = $(this).attr('href'); url = url.replace(/\//g, '#'); console.log(url); // replaces all }); ```