I have the following regex defined:
const regEx = new RegExp('[A][A-Z]{2}[0-2]{5}\b', 'g')
I have tested it in online regex testers and they all seem to work. I would like to search through a string for all words that look like this: ADB12210
. They have to start with an A followed by 2 upper case letters and end with 5 numbers from 0-2 and be a global search since this could appear multiple times in the string. This seems to work but when I try it by creating a pipe in angular it doesn't find any matches. The match returns null but I am expecting an array like: ['ADB12210', 'ACG12212']
. My pipe is as follows:
transform(value: string): any {
const regExp = new RegExp('[A][A-Z]{2}[0-2]{5}\b', 'g')
console.log(value.match(regExp)
}
Even if I hard code the check it still returns null:
const string = 'ACG12212'
console.log(string.match(regExp)
I have the following regex defined:
const regEx = new RegExp('[A][A-Z]{2}[0-2]{5}\b', 'g')
I have tested it in online regex testers and they all seem to work. I would like to search through a string for all words that look like this: ADB12210
. They have to start with an A followed by 2 upper case letters and end with 5 numbers from 0-2 and be a global search since this could appear multiple times in the string. This seems to work but when I try it by creating a pipe in angular it doesn't find any matches. The match returns null but I am expecting an array like: ['ADB12210', 'ACG12212']
. My pipe is as follows:
transform(value: string): any {
const regExp = new RegExp('[A][A-Z]{2}[0-2]{5}\b', 'g')
console.log(value.match(regExp)
}
Even if I hard code the check it still returns null:
const string = 'ACG12212'
console.log(string.match(regExp)
Share
Improve this question
edited Nov 12, 2020 at 5:57
Martheli
asked Nov 12, 2020 at 5:40
MartheliMartheli
9812 gold badges17 silver badges37 bronze badges
5
-
Try this:
regExp = /\b[A][A-Z]{2}[0-2]{5}\b/g
– anubhava Commented Nov 12, 2020 at 5:43 - @anubhava still no match – Martheli Commented Nov 12, 2020 at 5:46
- Try using \\b. You may need to backslash the backslash. – Frank Yellin Commented Nov 12, 2020 at 6:05
- @FrankYellin that was my problem. Now it works as expected. Create an answer and I will accept it. – Martheli Commented Nov 12, 2020 at 6:07
- @Martheli. Thanks. Done. – Frank Yellin Commented Nov 12, 2020 at 6:08
2 Answers
Reset to default 3You need to use \\b
, since so that a single backslash will make through into the string.
I removed the last boundary you have because I think your words may or may not have spaces in between and that might be causing it to be null, try with below.
value = 'asdasADB12210asd'
const regExp = new RegExp('[A][A-Z]{2}[0-2]{5}', 'g')
console.log(value.match(regExp))