我在javascript中有一个字符串:
const str = "bugfix/SOME-9234-add-company"; // output should be SOME-9234
const str2 = "SOME/SOME-933234-add-company"; // output should be SOME-933234
const str3 = "test/SOME-5559234-add-company"; // output should be SOME-5559234
我想提取SOME-..
直到第一个-
字符。
我使用此正则表达式,但没有用。什么是正确的正则表达式?
const s = "bugfix/SOME-9234-add-company";
const r1 = s.match(/SOME-([1-9])/);
const r2 = s.match(/SOME-(.*)/);
const r3 = s.match(/SOME-(.*)-$/);
console.log({ r1, r2, r3 });
您可以使用/(SOME-[\d]+)/g
正则表达式,例如
const strings = [
"bugfix/SOME-9234-add-company", // output should be SOME-9234
"SOME/SOME-933234-add-company", // output should be SOME-933234
"test/SOME-5559234-add-company" // output should be SOME-5559234
];
strings.forEach(string => {
const regex = /(SOME-[\d]+)/g;
const found = string.match(regex);
console.log(found[0]);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句