简体   繁体   English

从 textarea 中删除带有换行符的方大括号内的内容

[英]Remove content within square curly brackets with linebreak from textarea

I have the following textarea on a create forum post page.我在创建论坛帖子页面上有以下文本区域。

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.

The confidential information is automatically pasted into the textarea when the user fills in the form.当用户填写表单时,机密信息会自动粘贴到 textarea 中。 But now I want to add a button that allows posting without site details.但现在我想添加一个按钮,允许在没有网站详细信息的情况下发布。 But still allows for confidential information.但仍然允许机密信息。

Personally, I am terrible at regular expressions.就个人而言,我在正则表达式方面很糟糕。 When looking at those it is like magic for me.看着那些对我来说就像魔术一样。 But I am pretty sure that it is exactly what I need.但我很确定这正是我所需要的。 So I am now trying with the code below but this is deleting everything but randomly.所以我现在正在尝试使用下面的代码,但这是随机删除所有内容。

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

I was really hoping if someone could help me with removing this part from the textarea but keep everything else:我真的希望有人能帮我从 textarea 中删除这部分,但保留其他所有内容:

Needs to be removed需要移除

[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]

So the result is like this所以结果是这样的

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.

These parts are the start and end of what needs to be removed.这些部分是需要删除的部分的开始和结束。 Please keep in mind that there are linebreaks(enters) in the textarea请记住,textarea 中有换行符(回车)

Start开始

[confidential]
{sitedetails}

End结尾

{/sitedetails}
[/confidential]

The regex you want is /\\s*\\[confidential\\]\\s*\\{sitedetails\\}(\\s|\\S)*{\\/sitedetails\\}\\s*\\[\\/confidential\\]\\s*/g .你想要的正则表达式是/\\s*\\[confidential\\]\\s*\\{sitedetails\\}(\\s|\\S)*{\\/sitedetails\\}\\s*\\[\\/confidential\\]\\s*/g

It's a bit verbose, but hover over each part in regex101 to understand what's going on: https://regex101.com/r/R5Oece/1这有点冗长,但将鼠标悬停在 regex101 中的每个部分以了解发生了什么: https ://regex101.com/r/R5Oece/1

But I am pretty sure that it [regex] is exactly what I need.但我很确定它 [regex] 正是我所需要的。

Regex is rarely necessary, it's a convenience tool, but you could just as easily split your text by new lines, spliced out the sub-array, and rejoined into a string.正则表达式很少需要,它是一个方便的工具,但您可以轻松地将文本按新行拆分,拼接子数组,然后重新加入字符串。

 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.

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