Search code examples
regexservicenow

I'm trying to use regex to specify amount of characters. The curly brackets are not working


var validRegExp = /^[0-9]+[-]+[0-9]+$/;

This is my script, and what I want is:

var validRegExp = /^[0-9]{5}+[-]+[0-9]{2}+$/;

But when I put this in it doesn't like it.

I want 5 numbers, followed by a dash, followed by two numbers


Solution

  • When you write :

    [0-9]{5}+
    

    It's no correct because you can translate it as a numerical 5 times, 1 or more. As you already write 5 times the + shouldn't be used.

    The correct regex would be :

    /^[0-9]{5}(-)+[0-9]{2}$/;