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

发布于 2024-12-01 09:07:37 字数 396 浏览 2 评论 0原文

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

<div> Some text (these are the non-ascii encoded parenthesis).
<div>

我想去掉括号。我有以下内容,我在其他地方使用它来清理一些 html 元素,但我无法获得类似于删除实际文本的工作:

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

我发现了一些想法,但它们处理普通文本。摆脱括号是一个挑战,因为我不确定如何编写括号以便 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二货你真萌 2024-12-08 09:07:37

您应该使用普通 Javascript 进行替换/清理。类似的事情

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

就会做到。请注意,这将查询整侧的所有

节点,您希望对选择器更加具体。

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

如果您想删除括号和中间的所有内容,您可以只需将正则表达式更改为 /\(.*?\)/g 即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文