[英]Remove content within square curly brackets with linebreak from textarea
我在创建论坛帖子页面上有以下文本区域。
There is some text here in front:
[confidential]
{sitedetails}
Site URL: aaa
Site Username: bbb
Site Password: cc
FTP URL: ddd
FTP Username: eee
FTP Password: ffff
Optional Information: gggg
{/sitedetails}
[/confidential]
There is some text in the middle.
[confidential]
User added some confidential details. This should not be removed!!!
[/confidential]
And there is some text at the end.
当用户填写表单时,机密信息会自动粘贴到 textarea 中。 但现在我想添加一个按钮,允许在没有网站详细信息的情况下发布。 但仍然允许机密信息。
就个人而言,我在正则表达式方面很糟糕。 看着那些对我来说就像魔术一样。 但我很确定这正是我所需要的。 所以我现在正在尝试使用下面的代码,但这是随机删除所有内容。
var $editor = $(".markItUpEditor");
var curValue = $editor.val();
val = curValue;
val = val.replace(/'[confidential]'([\s\S]*)[\/confidential]/gm, "");
val = val.replace(/[[\]]/g,'')
$editor.val(val);
console.log(val);
我真的希望有人能帮我从 textarea 中删除这部分,但保留其他所有内容:
需要移除
[confidential]
{sitedetails}
Site URL: aaa
Site Username: bbb
Site Password: cc
FTP URL: ddd
FTP Username: eee
FTP Password: ffff
Optional Information: gggg
{/sitedetails}
[/confidential]
所以结果是这样的
There is some text here in front:
There is some text in the middle.
[confidential]
User added some confidential details. This should not be removed!!!
[/confidential]
And there is some text at the end.
这些部分是需要删除的部分的开始和结束。 请记住,textarea 中有换行符(回车)
开始
[confidential]
{sitedetails}
结尾
{/sitedetails}
[/confidential]
你想要的正则表达式是/\\s*\\[confidential\\]\\s*\\{sitedetails\\}(\\s|\\S)*{\\/sitedetails\\}\\s*\\[\\/confidential\\]\\s*/g
。
这有点冗长,但将鼠标悬停在 regex101 中的每个部分以了解发生了什么: https ://regex101.com/r/R5Oece/1
但我很确定它 [regex] 正是我所需要的。
正则表达式很少需要,它是一个方便的工具,但您可以轻松地将文本按新行拆分,拼接子数组,然后重新加入字符串。
const str = `There is some text here in front: [confidential] {sitedetails} Site URL: aaa Site Username: bbb Site Password: cc FTP URL: ddd FTP Username: eee FTP Password: ffff Optional Information: gggg {/sitedetails} [/confidential] There is some text in the middle. [confidential] User added some confidential details. This should not be removed!!! [/confidential] And there is some text at the end.`; let out = str.replace(/\\s*\\[confidential\\]\\s*\\{sitedetails\\}(\\s|\\S)*{\\/sitedetails\\}\\s*\\[\\/confidential\\]\\s*/g, '\\n\\n'); console.log(out);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.