Reputation: 2455
I would like to fetch one item in the string,
the main string is : This is an inactive AAA product. It will be replaced by replacement AAAA/BBBB number ABX16059636/903213712 during quoting
I would like fetch ABX16059636/903213712,
is there any way to achieve this one using Regex
?
Pls share some suggestions pls.
Upvotes: 1
Views: 157
Reputation: 1516
This is the result:
const regex = /number ([^ ]+) /;
const str = `This is an inactive AAA product. It will be replaced by
replacement AAAA/BBBB number ABX16059636/903213712 during quoting`;
let m;
if ((m = regex.exec(str)) !== null) {
let match = m[1];
console.log(`Found match: ${match}`);
}
The regex itself can be read as:
number
[^ ]
means "Anything except for space characters"+
means "One or more of these"Do use https://regex101.com/r/ARkGkz/1 and experiment with this.
Upvotes: 0
Reputation: 3917
Try the code below.It will show your matches as well as matches group.
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}`);
});
}
Upvotes: 1
Reputation: 687
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)
This regular expression matches any 3 alphabets followed by one ore more digits followed by / and then one or more digits.
Upvotes: 1
Reputation: 7476
Try with below regex,
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)
Upvotes: 2