简体   繁体   中英

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. 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:

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

Start

[confidential]
{sitedetails}

End

{/sitedetails}
[/confidential]

The regex you want is /\\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

But I am pretty sure that it [regex] is exactly what I need.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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