Reputation: 477
How do I remove the "/" character and the text before it with jQuery? I know how to replace the character, but the text is generated dynamically so unsure how to do that. I guess I need to use regex somehow?
<span class="unfoldedlabel" colspan="6"><a>Accessories/Service & Support</a></span>
Upvotes: 0
Views: 4914
Reputation: 6742
A combination of the best bits of the answers/comments so far:
$('.unfoldedlabel a').text(function(_, text) {
// return (text.split('/').pop()); // just the original requirement
return (text.split('/').pop().replace(/\s*/g,'')); // with whitespace removed
});
Upvotes: 2
Reputation: 1
Try using String.prototype.match()
with RegExp
/([^\/]+$)/
to negate forward slash character "/"
, match any characters following forward slash character until end of input string
document.querySelector(".unfoldedlabel")
.textContent = document.querySelector(".unfoldedlabel").textContent
.match(/([^\/]+$)/)[0]
<span class="unfoldedlabel" colspan="6"><a>Accessories/Service & Support</a></span>
Upvotes: 0
Reputation: 1
You can use split("/")
;
var newString = oldString.split("/");
newString
is then an array of the content of the oldString
with newString[0]
the text before / and newString[1] the text after.
Upvotes: 0
Reputation: 2420
If you're certain you only have 1 slash:
var str = "Accessories/Service & Support";
str = str.split("/").pop();
alert(str);
http://jsfiddle.net/jEQQg/904/
Upvotes: 4