开发者

How Do I Remove a text Parenthesis Using JQuery?

I have some auto-generated text that includes non-ascii encoded parentheses. For example:

<div> Some text (these are the non-asc开发者_Go百科ii encoded parenthesis).
<div>

I want to get rid of the parenthesis. I have the following, which I use elsewhere to clean out some html elements, but I can't get similar to work to remove actual text:

     jQuery(document).ready(function(){jQuery(".block").find("p").remove()});

I've found a few ideas around, but they deal with normal text. Getting rid of a parenthesis is a challenge, as I'm not sure how to code the parenthesis so that jQuery understands it.

Any ideas?


You should do the replacing/cleaning with vanilla Javascript. Something like

$('div').text(function(_, text) {
    return text.replace(/\(|\)/g, '');
});

will do it. Notice, this would query for all <div> nodes on the entire side, you want to be more specific on the selector.

demo: http://jsfiddle.net/2gHh2/

If you'd like to remove the parenthesis and everything in between, you'd simply have to change the regular expression to /\(.*?\)/g.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜