我想获取字符串中的一项,
主要字符串是:This is an inactive AAA product. It will be replaced by replacement AAAA/BBBB number ABX16059636/903213712 during quoting
我想获取ABX16059636/903213712,,有什么方法可以用Regex来实现吗?
请分享一些建议。
发布于 2017-05-30 13:27:56
尝试使用以下正则表达式,
var string = "This is an inactive AAA product. It will be replaced by replacement AAAA/BBBB number ABX16059636/903213712 during quoting"
var result = string.match(/[A-Z]+[0-9]+\/[0-9]+/g)
console.log(result)
发布于 2017-05-30 13:30:41
var s = 'This is an inactive AAA product. It will be replaced by replacement AAAA/BBBB number ABX16059636/903213712 during quoting'
var pat = /[A-Z]{3}\d+\/\d+/i
pat.exec(s)这个正则表达式匹配任何3个字母,后面跟着一个或多个数字,然后是/,然后是一个或多个数字。
发布于 2017-05-30 13:35:52
尝试代码below.It将显示您的匹配项以及匹配组。
const regex = /[A-Z]+[0-9]+\/+[0-9]+/g;
const str = `This is an inactive AAA product. It will be replaced by replacement AAAA/BBBB number ABX16059636/903213712 during quoting`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
https://stackoverflow.com/questions/44253998
复制相似问题