如何用jQuery刪除「/」字符和之前的文本?我知道如何替換角色,但文本是動態生成的,因此不確定如何去做。我想我需要使用正則表達式嗎?用jQuery刪除字符前的所有文本
<span class="unfoldedlabel" colspan="6"><a>Accessories/Service & Support</a></span>
如何用jQuery刪除「/」字符和之前的文本?我知道如何替換角色,但文本是動態生成的,因此不確定如何去做。我想我需要使用正則表達式嗎?用jQuery刪除字符前的所有文本
<span class="unfoldedlabel" colspan="6"><a>Accessories/Service & Support</a></span>
的答案/最好的評論位的組合至今:
$('.unfoldedlabel a').text(function(_, text) {
// return (text.split('/').pop()); // just the original requirement
return (text.split('/').pop().replace(/\s*/g,'')); // with whitespace removed
});
如果你確定你只有1條斜線:
var str = "Accessories/Service & Support";
str = str.split("/").pop();
alert(str);
您可以使用split("/")
;
var newString = oldString.split("/");
newString
是則oldString
與newString[0]
的內容的陣列之前的文本/和後newString [1]中的文本。
嘗試使用String.prototype.match()
與RegExp
/([^\/]+$)/
否定斜槓字符"/"
,符合下列提出任何字符,反斜槓字符,直到輸入字符串的結尾
document.querySelector(".unfoldedlabel")
.textContent = document.querySelector(".unfoldedlabel").textContent
.match(/([^\/]+$)/)[0]
<span class="unfoldedlabel" colspan="6"><a>Accessories/Service & Support</a></span>
你介意使用子字符串,indexOf從普通的JavaScript? – user2473779
類似'$('。unfoldedlabel a')。text(function(_,text){ return text.split('/')[1]; });'可能會幫助 – Satpal
檢查此http:// jsfiddle.net/0d104m3d/ – guradio