[英]Parsing text for specific values to create JSON object using Javascript
I need to convert text file entries into a JSON object however I am having trouble understanding how to look for certain values. 我需要将文本文件条目转换为JSON对象,但是我在理解如何查找某些值时遇到了麻烦。 Given the text document sample: 给定文本文档示例:
"get rf_da “获取rf_da
Device Timestamp, 6/16/2015 2:14:48 PM" 设备时间戳记,2015年6月16日2:14:48 PM”
How would I find the value of the date and the string following "get" using Javascript. 我如何才能找到的日期和字符串的值下面的 “得到”使用Javascript。
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'. 一种方法是使用正则表达式,例如,以下行将检索“ 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/ 这是您可以使用的便捷在线正则表达式测试仪: https : //regex101.com/
For more details, see the following links: How do you access the matched groups in a JavaScript regular expression? 有关更多详细信息,请参见以下链接: 如何在JavaScript正则表达式中访问匹配的组?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.