Angular regex that matches a pair of words between ->


I need a regex that matches texts with one or more lines from here:

"" (empty chain)


word1 -> word2

succesives \r\s\t


word1 and word 2 can be a combo of digits ; # or any symbol excepting \n.

I’ve proved this:

const expreg = new RegExp('([^"->"\n]->[^"->"\n])|(([^"->"\n]->[^"->"\n])\n)*');

But it also matches


second try: but it doesn’t even match hello1->hello2



You could match 1 or more word characters, and then have a repeating part matching -> followed by 1 or more word characters again.

The whole expression is optional to also match an empty string.


See a regex demo

const expreg = /^(?:[^>-]+[^\S\r\n]*->[^\S\r\n]*[^>-]+)?$/m;
  "word1  ->   word2",
  "my django db->my django db",
  "word1  ->   word2 ->word3"
].forEach(s => console.log(`${expreg.test(s)} "${s}"`));

Answered By – The fourth bird

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published