Alfonso
Alfonso

Reputation: 1325

Regex expression: Starts with either digits/asterisks or a '+' sign, contains a min of 2 digits and ends with digits/asterisks

The conditions of the regex that I'm trying to figure out are this:

Example valid strings:

240*******
+12*
216438827*
*164*8827*
********21

I have been a few hours trying to solve it, but I'm stuck at this regex expression /^\+?[\d*]{2,}$/, I cannot figure out how to set the 2 min. digits requirement, so I'll really appreciate some help, thanks!

A related screenshot:

example screenshot

Upvotes: 1

Views: 645

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

You may use

^\+?(?:\**\d){2,}\**$

See the regex demo

Details

  • ^ - start of string
  • \+? - an optional +
  • (?:\**\d){2,} - two or more occurrences of:
    • \** - 0+ * chars
    • \d - a digit
  • \** - a * symbol
  • $ - end of string.

Upvotes: 1

Related Questions