1

I am trying to remove everything between two words with brackets. The words are: {start} and {end}

  (function ($) {
    $(document).ready(function(){

    $('.well.save').on('mouseleave touchend', function(){
        // alert('mouseleave touchend');
        var $editor = $(".markItUpEditor");
        var curValue = $editor.val();
        //alert(curValue);

        // check
        var confCheck = curValue.includes("{start}");
        //alert(confCheck);
        if (confCheck == true) {


          var myStr = $editor.val();
          var subStr = myStr.match("{start}(.*){end}");
          alert(subStr[1]);
          //$editor.val(curValue);

        }
    });
  })(jQuery);

The code above returns null.

I am getting the content from a textarea. This textarea has a section with text added on mouseleave. This section starts with the word {start} and ends with the word {end} now I want to remove everything between those two words and the words as well so that on the next mouse leave I can re-add updated information.

Jquery version is from native Joomla 3. And the textarea contains the following:

There is some text in the message!


[confidential]
{start}

Site URL: 
Site Username: 
Site Password: 

FTP URL: 
FTP Username: 
FTP Password: 

Optional Information: 

{end}
[/confidential]```
purple11111
  • 709
  • 7
  • 20
  • 1. Please click [edit](https://stackoverflow.com/posts/56702851/edit) 2. click `[<>]` snippet editor 3. add RELEVANT JS (string examples and regex) to the JS pane. There is no jQuery relevance in your question – mplungjan Jun 21 '19 at 11:56
  • @mplungjan `add RELEVANT JS (string examples and regex)` What do you mean by this? Do you want an example of the textarea? – purple11111 Jun 21 '19 at 11:57
  • 1
    I want an example of the input strings and the rexexes and expected output. I have edited your question since once you have the string, there is nothing related to jQuery so please remove it and the tag to get proper answers – mplungjan Jun 21 '19 at 11:58
  • @mplungjan Please don't remove most of the question that may not be relevant to you but could be relevant for others. As to the strings I can put an example in. But I don't know about the rexexes. – purple11111 Jun 21 '19 at 11:59
  • Except that it happens in a block of jQuery code. – purple11111 Jun 21 '19 at 12:01
  • I understand what you are saying. And I already remove all the code that is not relevant. Now I understand that the answer to my question could very well be in a pure Javascript way but I also understand that to have a full picture the entire code block is needed what I have published. And that includes the jQuery. – purple11111 Jun 21 '19 at 12:05
  • 1
    But it is not. If you REALLY wanted the complete jQuery, you need to provide us with a [mcve] which includes the jQuery version your use and the editor plugin in case it manipulates the {start} and {end} before passing it to your click. – mplungjan Jun 21 '19 at 12:07

2 Answers2

2

Your text area content might also content new lines, and hence the given regex won't work in my view. The regex which will capture everything between the {start} and {end} would be :

/{start}([\s\S]*){end}/gm

https://regex101.com/r/AystH8/1

To delete all the content between the two keywords including them, use following:

//If your string is in the variable val;
val = "hi, my name is {start}\n \n gibberish and wrong \ncontent {end} prime hit!";
val = val.replace(/{start}([\s\S]*){end}/gm, "");
console.log(val); // output would be : hi, my name is prime hit!

I hope this answers your question.

prime_hit
  • 326
  • 1
  • 7
  • I don't know why but my output is the entire string? – purple11111 Jun 21 '19 at 12:21
  • 1
    Sorry, my bad! replace function returns a new string and cannot modify the string because they are immutable. We have to set the val = val.replace (..) . Please see the updated answer. Is it working now @purple11111 – prime_hit Jun 21 '19 at 12:22
  • Yes, indeed it works flawlessly. Thank you very much for your answer! – purple11111 Jun 21 '19 at 12:27
  • 1
    I wished I could accept two answers. As yours deserves acceptance as well. But I had to go with mplungjan as he not only answered my question but also thought me a thing or two about SO questions and his code fits in more with the rest of my code. But nevertheless your answer is also a very good working solution. Thank you very much! – purple11111 Jun 21 '19 at 12:33
1

Once you have the string, the code is as follows - Your final edit gave me the hint that you have a multiline string. Here is the code to fix it

https://regex101.com/r/4MYLO3/3

Remove between - MULTILINE

var re = /{start}([\S\s]*?){end}/gm 

var str = $(".editor").val()
var newStr = str.replace(re,"");
console.log(newStr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<textarea class="editor" rows="10">There is some text in the message!
    [confidential]
    {start}
    
    Site URL: 
    Site Username: 
    Site Password: 
    
    FTP URL: 
    FTP Username: 
    FTP Password: 
    
    Optional Information: 
    {end}
    [/confidential]</textarea>

Copy from between

var re = /{start}([\S\s]*?){end}/gm 

var str = $(".editor").val()
var newStr = str.match(re);
console.log(newStr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<textarea class="editor" rows="10">There is some text in the message!
    [confidential]
    {start}
    
    Site URL: 
    Site Username: 
    Site Password: 
    
    FTP URL: 
    FTP Username: 
    FTP Password: 
    
    Optional Information: 
    {end}
    [/confidential]</textarea>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Thanks for your answer. But it is the other way around. `{start} and the {end}` is the part that needs to be removed. So the result would be: Please get the data between – purple11111 Jun 21 '19 at 12:09
  • I tested your remove between function but the console output is the entire textbox. I made a edit to my post and included the content of the textbox. – purple11111 Jun 21 '19 at 12:18
  • Thank you very much for all the effort. Could I use `var str = $editor.val();` ? – purple11111 Jun 21 '19 at 12:23
  • 1
    Yes you certainly can if that is how the plugin works – mplungjan Jun 21 '19 at 12:24
  • 1
    YES IT DOES. Oh thank you so much! – purple11111 Jun 21 '19 at 12:25
  • You know what is funny. It is a duplicate but it never came up for all the things that I typed in. I guess that the more familiar someone is with something the more direct the questions are. – purple11111 Jun 21 '19 at 12:30
  • 1
    Sure. But also as I said. there was nothing related to jQuery in your question. It was all pure regex. So next time try to boil it down to the actual question. If the jQUery works, then no need to post it – mplungjan Jun 21 '19 at 12:32
  • 1
    You are right! But the main reason I put that stuff in is because it could be usefull for others. A sort of giving all the puzzle pieces. It was my bad though that I forgot to put in the actual textarea content. That was a stupid and time costing mistake. So thanks again for baring with me! – purple11111 Jun 21 '19 at 12:34
  • @purple11111 I gave you the jQuery needed now. – mplungjan Jun 21 '19 at 12:37
  • Thank you. I modified the code you provided to work with the rest of the code I already had and it is working like a champ! Thanks so much for answering, guiding and overall helping me out! Superb effort. – purple11111 Jun 21 '19 at 12:54
  • You already helped me a lot but it would be really cool if you could give an example on how to keep between multiline. – purple11111 Jun 21 '19 at 15:20
  • 1
    Ok - I’ll have look later – mplungjan Jun 21 '19 at 15:27
  • Super. Looking forward to it. Thanks! – purple11111 Jun 21 '19 at 15:28
  • I made a separate question from it and I tried to keep it as short and necessary as you thought me today. If you want you could reply there the solution. But please do it whenever you feel like it. I did not write this message to push you just to point out the link to the question: https://stackoverflow.com/questions/56706868/extracting-between-two-words-from-multiline-textarea Also if you have more pointers to writing a good question or what I did wrong with this new question I would love to hear that. Anyway thanks a lot and have a great weekend! – purple11111 Jun 21 '19 at 16:12