Following regex should work fine:
/(?=^.{3,28}$)[^\W_]+\-[^\W_]+/
var array = [
"123456790-123456789012345678",
"123456790-1234567890123456789",
"adsd-dsds"
];
var re = /(?=^.{3,28}$)[^\W_]+\-[^\W_]+/;
array.forEach(e => console.log(re.test(e) ? e.match(re)[0]: "match failed"));
Breakdown shamelessly copied from regex101.com:
- Positive Lookahead
(?=^.{3,28}$)
^ asserts position at start of a line
.{3,28} matches any character (except for line terminators)
{3,28} Quantifier — Matches between 3 and 28 times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of a line
- Match a single character not present in the list below
[^\W_]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\W matches any non-word character (equal to [^a-zA-Z0-9_])
_ matches the character _ literally (case sensitive)
\- matches the character - literally (case sensitive)
- Match a single character not present in the list below
[^\W_]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\W matches any non-word character (equal to [^a-zA-Z0-9_])
_ matches the character _ literally (case sensitive)