0

I need to convert text file entries into a JSON object however I am having trouble understanding how to look for certain values. Given the text document sample:

"get rf_da

Device Timestamp, 6/16/2015 2:14:48 PM"

How would I find the value of the date and the string following "get" using Javascript.

Geno Diaz
  • 400
  • 1
  • 7
  • 21
  • why you don't send the serialized JSON from server? – lem2802 Jul 30 '15 at 18:39
  • The data resides in old text files. The code will never be used to parse anything other than documents that were created months ago. – Geno Diaz Jul 30 '15 at 18:41
  • You need to define specific rules in order to do anything like that. Artificial Intelligence is out of scope for this question :-) – Amit Jul 30 '15 at 18:53

1 Answers1

1

One way would be to use a regular expression, for example, the following line would retrieve the date string following the text after the 'get'.

var textFileLine = "get rf_da /n Device Timestamp, 6/16/2015 2:14:48 PM"; var lineAfterGetRegEx = /(get)([\w|\s|\n]+)(Device Timestamp,\s)([^"]+)/g; var lineAfterGetMatch = lineAfterGetRegEx.exec(textFileLine); alert(lineAfterGetMatch[4]);

From there, you can refine the regular expression above to retrieve the date. Here is a handy online regex tester you can use: https://regex101.com/

For more details, see the following links: How do you access the matched groups in a JavaScript regular expression?

Community
  • 1
  • 1
michaelok
  • 1,124
  • 1
  • 13
  • 20
  • hmmm figured regular expressions were the way to go. I was hoping for something like "grab the text after this keyword" type deal. But this will do fine thank you! – Geno Diaz Jul 30 '15 at 20:57