用于提取版本和文件名的节点正则表达式模式
Node regex pattern to extract version and filename
我需要正则表达式方面的帮助,以便从文件名字符串中提取名称和版本,例如:
"ABC V1.2.3+4.exe"
"file name with spacesV1.2.3+4.exe"
"etc...V1.2.3+4.exe"
版本部分始终采用 VX.Y.Z+B
格式,但名称('V' 之前的任何内容都可以)
我能够使用此正则表达式模式提取版本号:
/V(\d+)(\.\d+)(\.\d+)(\+\d+)?/g (build number is optional)
例如:
let file = "HelloWorld V4.5.6+7.exe";
console.log(file.match(/V(\d+)(\.\d+)(\.\d+)(\+\d+)?/g));
output: [ 'V4.5.6+7' ]
到目前为止一切顺利。但我也想要从字符串开头到匹配版本号的部分。
我希望输出为:
['whatever is before the matched version number', 'V4.5.6+7']
我不太擅长正则表达式,我花了 4 个小时尝试。
使用.*
匹配版本号前的所有内容。将捕获组放在正则表达式的前缀和版本部分周围。
不要使用 g
标志。这使得它 return 所有完整的匹配项,而不是一组捕获组。由于只能匹配一次,因此不需要全局标志。
let file = "HelloWorld V4.5.6+7.exe";
console.log(file.match(/^(.*)V(\d+\.\d+\.\d+(?:\+\d+)?)/));
我需要正则表达式方面的帮助,以便从文件名字符串中提取名称和版本,例如:
"ABC V1.2.3+4.exe"
"file name with spacesV1.2.3+4.exe"
"etc...V1.2.3+4.exe"
版本部分始终采用 VX.Y.Z+B
格式,但名称('V' 之前的任何内容都可以)
我能够使用此正则表达式模式提取版本号:
/V(\d+)(\.\d+)(\.\d+)(\+\d+)?/g (build number is optional)
例如:
let file = "HelloWorld V4.5.6+7.exe";
console.log(file.match(/V(\d+)(\.\d+)(\.\d+)(\+\d+)?/g));
output: [ 'V4.5.6+7' ]
到目前为止一切顺利。但我也想要从字符串开头到匹配版本号的部分。
我希望输出为:
['whatever is before the matched version number', 'V4.5.6+7']
我不太擅长正则表达式,我花了 4 个小时尝试。
使用.*
匹配版本号前的所有内容。将捕获组放在正则表达式的前缀和版本部分周围。
不要使用 g
标志。这使得它 return 所有完整的匹配项,而不是一组捕获组。由于只能匹配一次,因此不需要全局标志。
let file = "HelloWorld V4.5.6+7.exe";
console.log(file.match(/^(.*)V(\d+\.\d+\.\d+(?:\+\d+)?)/));