0

I have data numbers from, say, 101 to 1056. All starting with slash.

I want regexp checking if input suites conditions.

What I did is: /^\/[1-9]\d{2,3}$/

I assume that [101-1056] not the way it should be used? :-)

Is there a way to check if the number has four digits it can start with "1" only.

EagerToSing
  • 153
  • 11

2 Answers2

1

You can write the pattern as ^\/([1-9]\d{2,3})$ using a capture group for the number part and compare the values in JavaScript.

const regex = /^\/([1-9]\d{2,3})$/;
[
  "/101",
  "/456",
  "/1056",
  "/100",
  "/1057",
  "/test"
].forEach(s => {
  const m = s.match(regex);
  if (m) {
    const value = parseInt(m[1]);
    const inRange = value > 100 && value < 1057;
    const text = (inRange ? `` : `not `) + `in range for ${s}'`;
    console.log(text)
  } else {
    console.log(`no match for string '${s}'`)
  }
});
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

It's not recommended to use regex for numeric range validation. Using regular expressions to validate a numeric range

BTW, you can have something like this, check for 101-109, 110-199, 200-999, 1000-1049 and 1050-1056 separately.^(10[1-9]|1[1-9][0-9]|[2-9][0-9]{2}|10[0-4][0-9]|105[0-6])$