gpt4 book ai didi

javascript - 从javascript中的文本文件中读取特定单词

转载 作者:行者123 更新时间:2023-11-30 10:58:52 27 4
gpt4 key购买 nike

我有一个文件(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");

关于javascript - 从javascript中的文本文件中读取特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58814530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com