Shahir Abdullah
Shahir Abdullah

Reputation: 29

Regular expression with special characters and a specific string

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.

Upvotes: 1

Views: 36

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521339

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.

Upvotes: 2

Related Questions