Home > Blockchain >  Regular expression with special characters and a specific string
Regular expression with special characters and a specific string

Time:11-17

I want to create a regular expression for a string of maximum length 12.

The string is following this pattern: ####{value}#####

where # could be $, %, &, * and \n or space. And {value} is a constant string that will always be present. The number of # will vary.

For example

  1. ####{value}

  2. ###{value}#

  3. ##{value}##

  4. #{value}###

  5. {value}
    #### (with newline)

I came up with

/^([\*#$%&\s] {value} [\*#$%&\s]){0, 12}$/

but it's not working.

CodePudding user response:

I would use a positive lookahead to limit the length to a maximum of 12 characters.

^(?!.{13})[*#$%&\s]*. [*#$%&\s]*$

You may sandwich any value you wish between the two character classes of symbols.

  • Related