我有以下字符串:
@ eur99.def.mark.ocal:7342s / MWEB / Web的style.s.git#V4.2.0
如何才能获得以下数字4,2,0。
基本上我需要破折号后面的所有数字(#);
我已尝试过这种方式(使用模式后面的方式),但没有成功。
正则表达式:
(小于?=#)\ d +
注意:请注意,不是用字符串方法构建的JS
答案 0 :(得分:1)
直接进场:
var regex = /\d/g,
str = '"web": "@eur99.def.mark.ocal:7342s/mweb/web-style.s.git#v4.2.0"',
sl = str.substr(str.indexOf('#v')), // the needed slice
result = [];
while ((m = regex.exec(sl)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
result.push(m[0]);
}
console.log(result);

答案 1 :(得分:1)
使用括号记住匹配项,您可以在结果数组中访问它们(索引从1开始,整个匹配项存储在0中)
const str = '"web": "@eur99.def.mark.ocal:7342s/mweb/web-style.s.git#v4.22.514"'
const regExp = /#v([0-9]+)\.([0-9]+)\.([0-9]+)/
const res = regExp.exec(str)
console.log(res[1], res[2], res[3]) // 4 22 514
答案 2 :(得分:0)
您可以使用indexOf获取#
的索引
var str = "@eur99.def.mark.ocal:7342s/mweb/web-style.s.git#v4.2.0";
var num = str.substring(str.indexOf("#") + "#v".length);