如何使用JQuery删除文本括号? [英] How Do I Remove a text Parenthesis Using JQuery?

查看:74
本文介绍了如何使用JQuery删除文本括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些自动生成的文本,其中包含非ASCII编码的括号.例如:

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>

我想摆脱括号.我有以下内容,可以在其他地方使用这些内容来清除一些html元素,但无法获得与删除实际文本相似的功能:

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()});

我发现了一些想法,但是它们涉及普通文本.摆脱括号是一个挑战,因为我不确定如何编写括号以使jQuery能够理解.

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.

有什么想法吗?

推荐答案

您应该使用香草Javascript进行替换/清理.像

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

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

会做到的.请注意,这将查询整侧的所有<div>节点,您希望在选择器上更具体.

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

演示: http://jsfiddle.net/2gHh2/

如果要删除括号以及两者之间的所有内容,只需将正则表达式更改为/\(.*?\)/g.

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

这篇关于如何使用JQuery删除文本括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆