如何使用 jQuery 按位置删除字符串的一部分

发布于 2024-12-21 04:56:02 字数 419 浏览 2 评论 0原文

我需要通过插入符号位置删除文本区域值的一部分...这个值可以是这样

### something
### another
### 

,所以我想删除空白元素“###”。因为删除操作是通过按下 Enter 来调用的——所以在添加“\n”之前。我不能使用

.replace('### \n')

,因为没有'\n',如果没有它,JS将删除第一行中的“###”。

我想,我必须使用正则表达式,但我找不到它:(我尝试过使用类似的东西,

$(h.textarea).val($(h.textarea).val().replace('/(\s{2,}\#{3})(\s+)/g', ''));

你能帮我找到正确的表达式吗?

I'm need to delete a part of the textarea value by caret positions... This value can be

### something
### another
### 

so i want to remove the blank element "### ". because the remove action calls by the Enter pressed — so before adding "\n". and i can't use

.replace('### \n')

because there is no '\n', and without it, the JS will remove "### " in the first line.

i think, i must use regular expression, but i cannot find it :( i've tried to use something like this

$(h.textarea).val($(h.textarea).val().replace('/(\s{2,}\#{3})(\s+)/g', ''));

can you help me to find the correct expression?

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

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

发布评论

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

评论(2

瞄了个咪的 2024-12-28 04:56:02

试试这个

$('textarea').val($('textarea').val().replace(/(###)\s*$/g, ''));

你可以在这里查看:http://jsfiddle.net/sbPGc/

Try this instead

$('textarea').val($('textarea').val().replace(/(###)\s*$/g, ''));

You can check it out here: http://jsfiddle.net/sbPGc/

岛徒 2024-12-28 04:56:02

尝试使用 .replace('^#{3}\s*$', '') 代替。 ^ 匹配行开头,$ 匹配行结尾。

Try using .replace('^#{3}\s*$', '') instead. ^ matches line begining and $ matches line end.

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