Home > database >  write regex for the string starting with digits and ending with $ and the total length of string is
write regex for the string starting with digits and ending with $ and the total length of string is

Time:02-21

I tried

/^[0-9](\w)*[$]{4,}$/ 

but it only accepts the string of having $$$$ at last.

CodePudding user response:

This should meet your requirements:

/^(?=.{4,})\d \$$/

The positive lookahead assertions can help you with restricting the length of string

CodePudding user response:

You can start the match with a digit, then match 2 or more word characters and end the match with a dollar sign.

That way the pattern has to match at least 4 characters.

^[0-9]\w{2,}\$$

Regex demo

If the whole string should be at least 4 characters, start with a digit and end with a dollar sign, you can use the dot to match any character.

^[0-9].{2,}\$$
  • Related