-4

I'm trying to check a string which length of non-numeric and non-punctuation characters is between 3 and 10. But I think it's hard to manage it in just one regular expression, so how can I do that?

For example:

,,,,,,1231 is not ok, because length of non-numeric and non-punctuation characters is 0.

1abcdefghij111 is ok, because non-numeric and non-punctuation characters part is abcdefghij, the length of that is 10.

zuraq
  • 53
  • 1
  • 4
  • Is that between 3 and 10 inclusive? E.g. can the character occur 3 times to 10 times; or 4 times to 9 times; or some combination of the two? Also, are the non-numeric & non-punctuation characters always sequential/touching each other? – tonitone120 Sep 07 '20 at 04:12
  • @tonitone120 the character can occur 3 times to 10 times, and the non-numeric & non-punctuation characters can be everywhere – zuraq Sep 07 '20 at 06:03

1 Answers1

0

Match the start of the string, then zero or more numeric or punctuation characters in a character set.

Match one non-numeric, non-punctuation character, then match zero or more numeric or punctuation characters. Repeat the above 3 to 10 times, then match zero or more numeric or punctuation characters until you get to the end of the string.

const test = (str) => {
  console.log(/^[\d.,]*(?:[^\d.,][\d.,]*){3,10}[\d.,]*$/.test(str));
};
test(',,,,,,1231'); // false
test(',,,,,,aa'); // false
test(',,,,,,aaa'); // true
  • ^[\d.,]* - Start of string and possible leading characters

  • (?:[^\d.,][\d.,]*){3,10} - Match a non-digit, non-punc character 3 times, possibly interspersed with digit or punc characters

  • [\d.,]*$ - End of string and possible trailing characters `

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320