使用jQuery

时间:2015-11-17 10:08:28

标签: jquery regex

如何删除" /" jQuery之前的字符和文本?我知道如何替换字符,但文本是动态生成的,所以不确定如何做到这一点。我想我需要以某种方式使用正则表达式?

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

4 个答案:

答案 0 :(得分:2)

到目前为止,答案/评论的最佳位的组合:

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

答案 1 :(得分:1)

如果您确定只有1个斜杠:

var str = "Accessories/Service & Support";
str = str.split("/").pop();
alert(str);

http://jsfiddle.net/jEQQg/904/

答案 2 :(得分:0)

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

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

newStringoldString内容的数组,其中{/ 1}}之前是文本,而newString [1]是之后的文本。

答案 3 :(得分:0)

尝试将String.prototype.match()RegExp /([^\/]+$)/一起使用以取消正斜杠字符"/",匹配正斜杠字符后的任何字符,直到输入字符串结束

&#13;
&#13;
document.querySelector(".unfoldedlabel")
.textContent = document.querySelector(".unfoldedlabel").textContent
.match(/([^\/]+$)/)[0]
&#13;
<span class="unfoldedlabel" colspan="6"><a>Accessories/Service & Support</a></span>
&#13;
&#13;
&#13;