Home > Enterprise >  RegEx - allow whitespace at start and end of the string with fixed length
RegEx - allow whitespace at start and end of the string with fixed length

Time:02-03

Currently I am working on a regex with the following conditions:

  • Can accept whitespace at start and end of the string
  • Can accept hyphen in mid/end of the string
  • Length of the characters should not be more than 10 and less than 8 (excluding spaces)

I have created below regex but somehow length validation is not working as expected.

(^\s*?)((?!(-))([0-9-]*)){8,10}(\s*?)$ https://regex101.com/r/aJNmqj/1

CodePudding user response:

You should be able to achieve this with a simple regex like this: ^(\s*?)[0-9-]{8,10}(\s*?)$

I believe your other one didn't work because negative lookaheads can't be operated on by the quantifier {8,10} Fortunately, the negative lookahead isn't actually necessary.

Your regex got convoluted while you were trying to do this I bet, because it's happened to me too.

CodePudding user response:

From your original regex fragment [0-9-]* it looks like you want to allow only digits and dashes. Here is a solution that:

  • allows 8-10 digits or dashes
  • no dash in the beginning
  • may have leading and trailing spaces

Code with test cases:

const regex = /^(?!\s*-)\s*[0-9-]{8,10}\s*$/;
[
  '1234567',
  ' 123 456 7 ',
  '12345678',
  '1234567-',
  '123-5678',
  '-2345678',
  ' 12345678 ',
  ' 123 456 78 ',
  '12345678',
  '12345678 ',
  '12 34 56 78 ',
  '123456789',
  ' 123456789',
  ' 12 34 56 78 9',
  '1234567890',
  ' 123 456 7890',
  '12345678901'
].forEach(str => {
  let ok = regex.test(str);
  console.log('"'   str   '" ==> '   ok);
});

Explanation of regex:

  • ^ -- anchor at start of string
  • (?!\s*-) -- negative lookahead for optional spaces and dash
  • \s* -- optional spaces
  • [0-9-]{8,10} -- 8 to 10 digits/dashes
  • \s* -- optional spaces
  • $ -- anchor at end of string

Learn more about regex: https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex

  • Related