I have a particular Regex requirement for a Javascript project. The Regex should match all 10 letter words starting with 'A'
, having it's 5th letter 'I'
and the letter 'O'
occuring atleast once within the word.
I'm able to match 'A'
and 'I'
using /A...I...../i
but I'm unable to match the letter 'O'
which needs to occur atleast once in the word. Any help here is appreciated.
CodePudding user response:
Instead of trying to write black magic regexes, break down the problem into simpler pieces.
const input = "...";
const tenCharactersLong = input.length === 10;
const AinFirstPosition = input[0] === "A";
const IinFifthPosition = input[4] === "I";
const atLeastOneO = input.contains("O");
If you absolutely must use a regex, then...
const blackMagic = /(?=.*O)A...I.{5}/;
... should work fine.
CodePudding user response:
This regex will find words in a text that start with an A
, are 10 letters long, contain the letter O
, and have an I
as the 5th letter.
/\b[A](?=...I)(?=\w*O)[A-Z]{9}\b/ig
If it's just to match a complete string
/^[A](?=...I)(?=.*O)[A-Z]{9}$/i