3

How to store the search matches from id="post_body" to array?

<textarea name="post[body]" id="post_body">

--Lorem ipsum-- dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et --Dolore-- magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. --Duis aute-- irure dolor in reprehenderit in voluptate velit esse cillum --Dolore-- eu fugiat nulla pariatur

</textarea>

The target are all words surrounded by '--'. In Ruby, I do that with:

matches = tex.scan(/--(.+?)--/).flatten
matches = matches.uniq

In this case, the array would be:

matches = ["Lorem ipsum", "Dolore", "Duis aute"];

NB: the word "Dolore" appears twice surrounded with --, but in the array it should be stored only once.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
mguz
  • 57
  • 7
  • You start with `document.getElementById('post_body').value.match(/--(.+?)--/)` and then you reduce it down to get unique values. – Ja͢ck Jun 29 '15 at 07:25
  • *"How to store the search matches from id="post_body" to array?"* 1. Research. 2. Experimentation. 3. Attempt. (4. If necessary **after** 1-3: Post a question showing attempt with an [MCVE](/help/mcve), describe what you expected it to do, what it's doing instead, and ask why.) – T.J. Crowder Jun 29 '15 at 07:31

2 Answers2

3

    var text = document.getElementById('post_body').value;
    var res = text.match(/--\w+\s*\w*--/g);

     //remove duplicates
    uniqueArray = res.filter(function(item, pos, self) {
      return self.indexOf(item) == pos;
    });

     //remove "--" parts
    finalArray = uniqueArray.map(function(item) {
      return item.replace(/--/g,"");
    });

    alert(finalArray);
<textarea name="post[body]" id="post_body">--Lorem ipsum-- dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et --Dolore-- magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. --Duis aute-- irure dolor in reprehenderit in voluptate velit esse cillum --Dolore-- eu fugiat nulla pariatur</textarea>
Udith Gunaratna
  • 2,091
  • 1
  • 13
  • 17
1

You may use the following code:

function contains(a, obj) {
    var i = a.length;
    while (i--) {
       if (a[i] === obj) {
           return true;
       }
    }
    return false;
}

var re = /--\s*(.*?)\s*--/g; 
var str = '--Lorem ipsum-- dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et --Dolore-- magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. --Duis aute-- irure dolor in reprehenderit in voluptate velit esse cillum --Dolore-- eu fugiat nulla pariatur';
var arr = [];
while ((m = re.exec(str)) !== null) {
    if (!contains(arr, m[1]))
    { 
        arr.push(m[1]);
    }
 }
alert(arr);

The regex will match the text enclosed in -- and place the first capture group text into the array arr only if the value does not exist in the array (the contains function is taken from this SO post).

As for regex, \s* added to --\s*(.*?)\s*-- will trim the value inside. Remove these \s*s if you do not need trimming.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thank you very much! I'm starting with javascript and your answer helps me to understand how it works. I keep learning. – mguz Jun 29 '15 at 08:00
  • I am glad you liked it. It is just another approach: rather than performing a post-process, I am performing logical steps that exclude it (*1. Get the captured texts, 2. Check if the text is already in an array. 3. Add the string found to the array if not found in the array.*) If you found my answer helpful, please consider upvoting it (the up arrow on the left). – Wiktor Stribiżew Jun 29 '15 at 08:15