To find digits followed by optional uppercase letters, a forward slash, and more uppercase letters, use this regex pattern:
\b\d+(?:\.\d+)?[A-Z]*\/[A-Z]+\b
\b
This signifies a word boundary to prevent partial matches
\d+(?:\.\d+)?
Matches one or more digits with an optional decimal part
[A-Z]*
Matches any number of optional uppercase letters
\/[A-Z]+
Matches a forward slash followed by one or more uppercase letters
-\b
Another word boundary
Check out the Regex demo here
const regex = /\b\d+(?:\.\d+)?[A-Z]*\/[A-Z]+\b/;
[
"Apartment 101/B First",
"Villa 3324/A Second",
"Milk 12MG/ML Third",
"Sodium 0.00205MG/ML Fourth",
].forEach(s => {
const m = s.match(regex);
if (m) {
console.log(m[0]);
}
});
Edit
If you want to make the forward slash optional and only match digits, modify the pattern like this:
\b\d+(?:\.\d+)?[A-Z]*(?:\/[A-Z]+)?\b
\b
Signifies a word boundary
\d+(?:\.\d+)?
Matches one or more digits with an optional part
[A-Z]*
Matches any optional uppercase letters A-Z
(?:\/[A-Z]+)?
Optionally matches a forward slash and one or more uppercase letters A-Z
\b
Another word boundary
View the updated Regex demo here