-3

I am looking for a javascript where in I paste a content in textarea. The script must search for the timestamp of the format 01/07/2016 10:30:00 and chop it off. Please help me in this. It has a text content in which I have to eliminate the above thing. My html would be something like this-

<textarea id="quote" cols="50" rows="5" onkeyup="myFunction()">

</textarea>

<script>
function myFunction() {
    var x = document.getElementById("quote");
    x.value = x.value.toUpperCase();
}
</script>
swaroop reddy
  • 3
  • 1
  • 1
  • 5

2 Answers2

0

Try this regex

function myFunction() {
var x = document.getElementById("quote");
x.value = x.value.toUpperCase();
timestamp= x.value.match(/\d{2}\/\d{2}\/\d{4}\s\d{2}\:\d{2}\:\d{2}/g)
console.log(timestamp) //It will return array of timestamps found

}

Suhail Akhtar
  • 1,718
  • 15
  • 29
0

This is a regex approach which solves your problem.

var txtArea = document.getElementById('mytextarea');

txtArea.addEventListener('paste', function(e) {
  e.preventDefault();
  e.stopPropagation();

  clipboardData = e.clipboardData || window.clipboardData;
  pastedData = clipboardData.getData('Text');

  var pattern = /[\d]{2}\/[\d]{2}\/[0-9]{4}\s[\d]{2}:[\d]{2}:[\d]{2}/g;

  // remove date time pattern.
  var newData = pastedData.replace(pattern, '');
  
  // remove string 'LSDXW08'
  newData = newData.replace(/LSDXW08/g, '');
  
  // replace multiple spaces with 1 space
  newData = newData.replace(/\s{2,}/g, ' ');

  // append the value
  txtArea.value = txtArea.value + newData;
});
<textarea id="mytextarea" rows="8" cols="70"></textarea>

<h4> !! Try paste this !! </h4>
<p>
I am looking for a javascript where in I paste a content in textarea. The script must search for the timestamp of the format 01/07/2016 10:30:00 and chop it off. Please help me in this. It has a text content in which I have to eliminate the above thing. My html would be something like this-
</p>
choz
  • 17,242
  • 4
  • 53
  • 73
  • Exactly! But this replace gets stopped for the first search itself. The format appears multiple times in my text area. And also I need to eliminate an user Id as in LSDXW08 format which also appears multiple times in the content I pasted. – swaroop reddy Jul 22 '16 at 05:58
  • @swaroopreddy I dont get what you want.. Can you update your question? – choz Jul 22 '16 at 06:02
  • If I paste something in the textarea like this -"asfsdf LSDXW08 01/07/2016 10:30:00 sg ggf LSDXW08 01/07/2016 10:30:00 fsgfgfgghfd LSDXW08 01/07/2016 10:30:00" all the date and time along with the regex for "LSDXW08" must get eliminated and I should be able to see only "asfsdf sg ggf fsgfgfgghfd" . – swaroop reddy Jul 22 '16 at 06:37
  • Working great - @choz – swaroop reddy Jul 22 '16 at 09:01
  • ! Is there any way to read every line in the pasted content, search for, say a string and show the entire line if the expected string is present in the line? – swaroop reddy Jul 25 '16 at 10:42
  • @swaroopreddy If I get what you mean, Yes there is.. You can check the string condition line by line with regex as well.. – choz Jul 25 '16 at 10:45
  • I can search a regex pattern in the content, but how can I show the whole line if there is a match...? – swaroop reddy Jul 25 '16 at 11:28
  • @swaroopreddy It's hard to help without the context.. You should ask a new question instead.. – choz Jul 26 '16 at 01:02