0

I have a txt file which get read with javascript.

function handleTextFile(evt)
    {
        var files = evt.target.files; 
        var reader = new FileReader();
        reader.onload = function(){
        Text = reader.result.toLowerCase();

        };
        reader.readAsText(files[0]);
    }

I want find in var >Text< all Dates.

The located date shall be saved in a variable. The only thing i know-> i can match date formats with code

    var pattern =/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;

but i want not only a true or false output. I want the Value of this located date.

Anyone has a link or a some code for me?

  • Possible duplicate of [How do you access the matched groups in a JavaScript regular expression?](https://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression) – sabithpocker Jun 01 '17 at 13:34
  • nice. thank you very much – user2826395 Jun 01 '17 at 14:23

1 Answers1

0

Here is a working snipped with regex for dates in format 00-00-0000.

window.onload = function() {
  var fileInput = document.getElementById('fileInput');
  var fileDisplayArea = document.getElementById('fileDisplayArea');

  fileInput.addEventListener('change', function(e) {
   var file = fileInput.files[0];
   var textType = /text.*/;

   if (file.type.match(textType)) {
    var reader = new FileReader();

    reader.onload = function(e) {
          var myRegexp = /\b(\d{2}-\d{2}-\d{4})/g;
          var match = myRegexp.exec(reader.result);
     fileDisplayArea.innerText = match;
    }

    reader.readAsText(file); 
   } else {
    fileDisplayArea.innerText = "File not supported!"
   }
  });
}
<div id="page-wrapper">

  <h1>Your date reader.</h1>
  <div>
   Select a text file: 
   <input type="file" id="fileInput">
  </div>
  <pre id="fileDisplayArea"><pre>
</div>

I used this tutorial to assemble snipped above .

AESTHETICS
  • 989
  • 2
  • 14
  • 32