It might sound strange, but I have a full URL as a class for some of my HTML elements. Obviously, this doesn't work for CSS purposes. What I want to achieve is to remove everything except the page slug. Here's an example of what I currently have:
<li class="menu-item http://example.com/page-link/">some code</li>
What I would like in terms of HTML output is this:
<li class="menu-item page-link">some code</li>
I thought about using the .replace method to get rid of "http://example.com/", but I'm unsure how to remove the trailing slash and the beginning of the URL. Here's my idea:
var strclass = $(li[class*=http://example.com/]).attr('class').match(/\bhttp://example.com/.+?\b/);
var newclass = strclass.replace("http://example.com/", "").replace("/", "");
Any assistance would be greatly appreciated. If the code above seems off-track, please point me in the right direction with helpful resources.
******I'm exploring new methods and seeking additional feedback!******
This is the updated code that combines some great suggestions:
if(li.hasClass('menu-item')) {
var strclass = $('li.menu-item').attr('class');
var newclass = strclass.split(" ")[1].split("/")[3];
$(".menu-item").removeClass("http://example.com/" + strclass + "/").addClass(newclass);
}
The problem I am encountering now lies in the third line within the if statement where I try to replace the old class with the full URL with the new class containing only the page slug. If anyone has a solution or alternative approach, please share. Thank you all for your efforts and quick responses!