简体   繁体   English

如何使用JQuery删除文本括号?

[英]How Do I Remove a text Parenthesis Using JQuery?

I have some auto-generated text that includes non-ascii encoded parentheses. 我有一些自动生成的文本,包括非ascii编码的括号。 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: 我有以下内容,我在其他地方使用它来清理一些html元素,但我无法得到类似于删除实际文本的工作:

     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. 摆脱括号是一个挑战,因为我不知道如何编写括号,以便jQuery理解它。

Any ideas? 有任何想法吗?

You should do the replacing/cleaning with vanilla Javascript. 你应该用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. 请注意,这将查询整个端的所有<div>节点,您希望在选择器上更具体。

demo: http://jsfiddle.net/2gHh2/ 演示: 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 . 如果您想删除括号及其间的所有内容,则只需将正则表达式更改为/\\(.*?\\)/g

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM