admin管理员组

文章数量:1201171

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

<div> Some text (these are the non-ascii 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?

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

<div> Some text (these are the non-ascii 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?

Share Improve this question edited Aug 25, 2011 at 11:38 jAndy 236k57 gold badges311 silver badges363 bronze badges asked Aug 25, 2011 at 11:34 user624385user624385 3731 gold badge6 silver badges14 bronze badges 2
  • Do you want to get rid of the parentheses themselves, or everything they contain as well? – James Allardice Commented Aug 25, 2011 at 11:36
  • I'm just looking to ditch the parentheses, but it looks like Andy's answer below includes code to nix everything in between also. Thanks for replying. – user624385 Commented Aug 26, 2011 at 10:05
Add a comment  | 

1 Answer 1

Reset to default 25

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.

本文标签: javascriptHow Do I Remove a text Parenthesis Using JQueryStack Overflow