2015-11-17 25 views
-5

如何用jQuery刪除「/」字符和之前的文本?我知道如何替換角色,但文本是動態生成的,因此不確定如何去做。我想我需要使用正則表達式嗎?用jQuery刪除字符前的所有文本

<span class="unfoldedlabel" colspan="6"><a>Accessories/Service & Support</a></span> 
+0

你介意使用子字符串,indexOf從普通的JavaScript? – user2473779

+2

類似'$('。unfoldedlabel a')。text(function(_,text){ return text.split('/')[1]; });'可能會幫助 – Satpal

+0

檢查此http:// jsfiddle.net/0d104m3d/ – guradio

回答

2

的答案/最好的評論位的組合至今:

$('.unfoldedlabel a').text(function(_, text) { 
    // return (text.split('/').pop()); // just the original requirement 
    return (text.split('/').pop().replace(/\s*/g,'')); // with whitespace removed 
}); 
+0

這很好。是否有可能去除空白區域?這件事不是原始問題的一部分,但只是意識到這將是很好的知道。 – Xeptor

+0

@Xeptor只需使用'@ $。trim(text.split('/')。pop())' – Satpal

+0

@Satpal似乎不工作?不知道我是否寫了正確的,雖然 – Xeptor

0

您可以使用split("/");

var newString = oldString.split("/"); 

newString是則oldStringnewString[0]的內容的陣列之前的文本/和後newString [1]中的文本。

0

嘗試使用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>

相關問題