Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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-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?

like image 216
user624385 Avatar asked Aug 25 '11 11:08

user624385


1 Answers

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.

like image 99
jAndy Avatar answered Sep 23 '22 06:09

jAndy