title: Loop Function to find 14 digit numbers and run a script for those results - Javascript
body: <p><strong>Preface:</strong> I have two textareas. I want to paste a list of unformatted phone numbers to produce a list of phone numbers in a certain format. I figured out how to remove dashes, commas, spaces and letters to just produce the format I need. (I made it add a comma and a space and remove the last comma fyi)</p>
  <p><strong>Issue:</strong> The current code below works great in the order of the .replaces. I tried to group those together and it didn't work as well with some examples but breaking it up so it flows in that order seemed to work. However, I often have phone numbers of ranges. So if you have 555-555-5555 - 5556, it will output 55555555555556. 14 characters! I wondered if there was a way to &quot;find&quot; these 14 characters after this formula is ran and use another code I figured out (below the one below) to output the ranges for those numbers. I will post both codes below and finish my thought. Ideally, I want to combine both formulas.</p>
  <pre><code>&lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt;
  function textremv() {
  let output = Array.from(
      new Set(
          document.getElementById(&quot;content&quot;).value
          .replace(/,/g, &quot;&quot;) // replace commas with nothing
          .replace(/\n/g, &quot;,\n&quot;) //replaces new lines with comma and new line
          .replace(/\s|-/g, &quot;&quot;) // replaces space and dash with nothing
          .replace(/[{()}]/g,&quot;&quot;) //replaces all special characters with nothing
          .replace(/[a-zA-Z]/g, &quot;&quot;) //replaces a-z with nothing
          .replace(/,/g, &quot;,\n&quot;) //replaces comma with comma linebreak
          
          
          .split(',')
          
          
  
      )
  ).join(&quot;, &quot;);
  document.getElementById(&quot;content2&quot;).innerHTML = output + &quot; &quot;; //adds space at the end
  
  }
  
  // [0-9]{10}-[0-9]{4} 
  }
  function ct() {
    /* Get the text field */
    var copyText = document.getElementById(&quot;content2&quot;);
  
    /* Select the text field */
    copyText.select();
    copyText.setSelectionRange(0, 99999); /*For mobile devices*/
  
    /* Copy the text inside the text field */
    document.execCommand(&quot;copy&quot;);
  
    /* Alert the copied text */
    alert(&quot;Copied the text: &quot; + copyText.value);
    /* &lt;input type='button' value='Extract Text2' onclick='sortvalues(0);ct()'/&gt; */
  }
  &lt;/script&gt;
  
  &lt;form name=&quot;sorter&quot;&gt;
  &lt;textarea rows='10' cols='20' id='content'&gt;&lt;/textarea&gt;
  &lt;textarea rows='10' cols='20' id='content2'&gt;&lt;/textarea&gt;
  &lt;input type='button' value='Extract Text' onclick='textremv();ct()'/&gt;
  
  
  &lt;/form&gt;
  &lt;div id=&quot;output&quot;&gt;&lt;/div&gt;
  </code></pre>
  <p>Single range works with code below such as 5555555555-5556 will give both phone numbers listed out.</p>
  <pre><code>&lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt;
  function listrange(){
  var range = document.getElementById(&quot;listrange1&quot;).value
  var range1 = range.slice(0,10);
  var range2 = range.slice(0,7) + range.slice(12,15);
  var text = &quot;&quot;;
  var i = range1;
  while (i &lt;= range2) {
    text += i + &quot;, &quot;;
    i++;
  }
  document.getElementById(&quot;listrange2&quot;).innerHTML = text;
  }
  
  &lt;/script&gt;
  
  &lt;textarea rows='10' cols='20' id='listrange1'&gt;&lt;/textarea&gt;
  &lt;textarea rows='10' cols='20' id='listrange2'&gt;&lt;/textarea&gt;
  &lt;input type='button' value='Extract Text' onclick='listrange()'/&gt;
  </code></pre>
  <p><strong>Summary:</strong> I'm not sure if there is a way to combine the formula to take those 14 characters and then run it against the second script I have there to produce the list of phone number ranges.</p>
  <p>Example would be like this:</p>
  <p>555-555-5555
  123-456-7890-7891
  555-555-5506</p>
  <p>Should return an output of:
  5555555555,
  1234567890,
  1234567891,
  5555555506</p>
  <p>I am just not sure how to make it retrospectively go back and look for those 14 characters to apply that loop function.</p>
  <p>Thank you in advance!</p>
  <p><strong>Edit: Scott's answer worked!! In case you're coming across this question and are inexperienced as I am, here's the full code for you to play around with. This code assumes you are providing 10 digit phone numbers or 10 digits with a 4 digit range. Ex: 1234567890 or 1234567890-7899</strong></p>
  <pre><code>&lt;textarea rows='6' cols='50' id='listrange1'&gt;&lt;/textarea&gt;
  &lt;textarea rows='6' cols='50' id='listrange2'&gt;&lt;/textarea&gt;
  &lt;br&gt;
  &lt;button id='extract' &gt;Extract Phone Numbers&lt;/button&gt;
   
   &lt;script type=&quot;text/javascript&quot;&gt;
  const getRange = (str) =&gt; {
    const [_, intro, start, end] = str .match (/(\d{6})(\d{4})(\d{4})/)
    return Array.from({length: end - start + 1}, (__, i) =&gt; intro + String(+start + i).padStart(4, '0'))
  }
  
  const phoneNumbers = str =&gt; 
    str .split (/[,\s]+/)                    // break on commas and/or spaces
      .map (str =&gt; str .replace (/\D/g, '')) // remove non-digits
      .filter (Boolean)                      // remove empties
      .filter (str =&gt; str .length == 10 || str.length == 14)
      .flatMap (str =&gt; str .length == 14 ? getRange(str) : [str])
  
  document.getElementById('extract').onclick = (evt) =&gt; 
    document.getElementById('listrange2').innerHTML = 
      phoneNumbers(document.getElementById('listrange1').value).join('\n')
      &lt;/script&gt;
  </code></pre>
  <p>Searchable keywords: Javascript, phone number, ranges, phone number range, client-side, loop</p>