簡體   English   中英

從 javascript 中的文本文件中讀取特定單詞

[英]Reading a particular word from a text file in javascript

我有一個文件(version.h),其內容如下所示:

#ifndef _VERSION_H_
#define _VERSION_H_

// Date with short Git SHA hash
// '*' at end indicates changed files in repository.
#define _VERSION_STRING "2019-09-24-9uy8cbc"

#endif // _VERSION_H_

我想讀取版本值,即 _VERSION_STRING 值。 我正在考慮在文件中搜索單詞 _VERSION_STRING 並讀取它之后的值以獲取版本號。 有沒有其他方法可以做到這一點。 非常感謝任何幫助,因為我對 java 腳本非常陌生。謝謝!

我將使用以下正則表達式: /(?:\n|\r|^)#define\s+_version_string\s+"([^"]+)"(?:\r|\n|$)/

  • (?:\n|\r|^) :新行的開始(cr 或 lf)或文本的開始
  • #define\s+_version_string\s+ : 匹配字符串#define _VERSION_STRING考慮空格數
  • "([^"]+)" :匹配引號並獲取引號內的所有內容。
  • (?:\r|\n|$) :新行的開始(lf 或 cr)或文本的結尾

 var text = `#ifndef _VERSION_H_ #define _VERSION_H_ // Date with short Git SHA hash // '*' at end indicates changed files in repository. #define _VERSION_STRING "2019-09-24-9uy8cbc" #endif // _VERSION_H_`; var result = /(?:\n|\r|^)#define\s+_version_string\s+"([^"]+)"(?:\r|\n|$)/i.exec(text); console.log(result? result[1]: "version not found");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM