Issue
I need a regex that matches texts with one or more lines from here:
"" (empty chain)
word1->word2
word1 -> word2
succesives \r\s\t
\n
etc.
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
word1->
second try: but it doesn’t even match hello1->hello2
((^(\s\r\t|\n|->)^(->|\n)->^(\s|\r|\t|\n|->)^(->|\n))|(^(\s|\r|\t|\n|->)^(->|\n)->^(\s|\r|\t|\n|->)^(->|\n)\n)|(\n)|([\s\r\t]+)|([\s\r\t]+\n))
Solution
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.
^(?:[^>-]+[^\S\r\n]*->[^\S\r\n]*[^>-]+)?$
See a regex demo
const expreg = /^(?:[^>-]+[^\S\r\n]*->[^\S\r\n]*[^>-]+)?$/m;
[
"word1->word2",
"",
"word1 -> word2",
"my django db->my django db",
"tds->dfds\nmy->computer",
"word1->",
"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