0

I'm trying to find a Regex, that allows printable ASCII chars except - " / \ [ ] : ; | = , + * ? < >
The string length must be 1-25

This will work:

/^[^\\[\]\:\;\|\=\,\/+\*\?<>\"]{1,25}$/

But it will match also non-ASCII chars

cheziHoyzer
  • 4,803
  • 12
  • 54
  • 81
  • Are you sure a single regex is the right solution for your problem? If you are, that's fine; the regex is easy enough to write, but sometimes writing code can be more readable.... – Ray Toal Nov 15 '17 at 07:30
  • All ASCII or only printable ones? `[ -~]`? Also, what is the regex library? – Wiktor Stribiżew Nov 15 '17 at 07:33

1 Answers1

2

You may use

/^(?:(?![:[\];|\\=,\/+*?<>"])[ -~]){1,25}$/

See the regex demo

Details

  • ^ - start of string
  • (?: - outer grouping to enable matching a quantified lookahead-restricted ASCII char range
    • (?![:[\];|\\=,\/+*?<>"]) - the next char cannot be one defined in the character class (:, [, ], ;, |, \, =, ,, /, +, *, ?, <, > and ")
    • [ -~] - any printable ASCII
  • ){1,25} - 1 to 25 occurrences
  • $ - end of string
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Why you need '?:' ? it's looks it's working without it, can please give an example why you need it? – cheziHoyzer Nov 15 '17 at 08:41
  • 1
    @cheziHoyzer It works slightly slower when you use a *capturing* group since the regex engines writes the captured value to a separate memory buffer so that you could reference that value either from the pattern or the replacement string. To group a pattern in order to quantify it, a [*non-capturing* group](https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-what-does-a-question-mark-followed-by-a-colon) is a much more natural choice. – Wiktor Stribiżew Nov 15 '17 at 08:44