regex

How to check that string consists only from characters `A-z-—’', space and of length from 1 to 250 symbols?


I need a regex that checks that a string consists only from characters "A-z-—’'`", space and of length from 1 to 250 symbols.

I have a regex ^[A-z-—’'` ]{1,250}$ but it also finds strings like John[:

enter image description here

Why I allows strings with square brackets?


Solution

  • Please, note, that you should use a-zA-Z pattern for English letters, since current pattern A-z includes extra characters (consult ASCII table):

    A, B, ..., Z, [, \, ], ^, _, `, a, b, ..., z 
                  ^              ^
                  extra characters to be included
    

    So the correct pattern is

    ^[A-Za-z-—’'` ]{1,250}$