Angular Reactive Forms pattern validation: Invalid regular expression












1















I have a regex for Australia phone number validation working in an AngularJS website. I have set the exact pattern in the Reactive Forms validator as follows:



 Validators.pattern(
'/^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/'
)


The page doesn't load as it gets the following error:




Invalid regular expression: /^/^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/$/: Nothing to repeat




What is the correct way to specify this pattern when using Reactive Forms?










share|improve this question


















  • 1





    ^({0,1}...: the {0,1} quantifier repeats nothing. You should read a regex tutorial, in particular about quantifiers and character classes (to avoid useless alternations).

    – Casimir et Hippolyte
    Jan 29 '18 at 4:04
















1















I have a regex for Australia phone number validation working in an AngularJS website. I have set the exact pattern in the Reactive Forms validator as follows:



 Validators.pattern(
'/^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/'
)


The page doesn't load as it gets the following error:




Invalid regular expression: /^/^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/$/: Nothing to repeat




What is the correct way to specify this pattern when using Reactive Forms?










share|improve this question


















  • 1





    ^({0,1}...: the {0,1} quantifier repeats nothing. You should read a regex tutorial, in particular about quantifiers and character classes (to avoid useless alternations).

    – Casimir et Hippolyte
    Jan 29 '18 at 4:04














1












1








1








I have a regex for Australia phone number validation working in an AngularJS website. I have set the exact pattern in the Reactive Forms validator as follows:



 Validators.pattern(
'/^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/'
)


The page doesn't load as it gets the following error:




Invalid regular expression: /^/^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/$/: Nothing to repeat




What is the correct way to specify this pattern when using Reactive Forms?










share|improve this question














I have a regex for Australia phone number validation working in an AngularJS website. I have set the exact pattern in the Reactive Forms validator as follows:



 Validators.pattern(
'/^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/'
)


The page doesn't load as it gets the following error:




Invalid regular expression: /^/^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/$/: Nothing to repeat




What is the correct way to specify this pattern when using Reactive Forms?







regex angular validation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 29 '18 at 3:32









AnthonyAnthony

1,16311638




1,16311638








  • 1





    ^({0,1}...: the {0,1} quantifier repeats nothing. You should read a regex tutorial, in particular about quantifiers and character classes (to avoid useless alternations).

    – Casimir et Hippolyte
    Jan 29 '18 at 4:04














  • 1





    ^({0,1}...: the {0,1} quantifier repeats nothing. You should read a regex tutorial, in particular about quantifiers and character classes (to avoid useless alternations).

    – Casimir et Hippolyte
    Jan 29 '18 at 4:04








1




1





^({0,1}...: the {0,1} quantifier repeats nothing. You should read a regex tutorial, in particular about quantifiers and character classes (to avoid useless alternations).

– Casimir et Hippolyte
Jan 29 '18 at 4:04





^({0,1}...: the {0,1} quantifier repeats nothing. You should read a regex tutorial, in particular about quantifiers and character classes (to avoid useless alternations).

– Casimir et Hippolyte
Jan 29 '18 at 4:04












2 Answers
2






active

oldest

votes


















1














There are multiple issues:




  • The regex literal should not be used inside quotes (or use a string pattern with double escaped special chars)

  • Special chars like (, ) and + must be escaped - nothing to repeat is caused by the fact that ( (start of a capturing group) is quantified with {0,1} and that is an error


  • {0,1} is equal to ?, ( |-) can be written as [ -], (2|4|3|7|8) canbe written shorter as [23478].


So, you may use



Validators.pattern(
'^\(?(0|\+61)[24378]\)?[ -]?[0-9]{2}[ -]?[0-9]{2}[ -]?[0-9][ -]?[0-9]{3}$'
)


Note that you may even omit ^ and $ here since the anchors will be added by Angular automatically to the string pattern.



NOTE: if the separators should be consistent, capture the first one and then use backreferences to the group value:



Validators.pattern(
'\(?(?:0|\+61)[24378]\)?([ -]?)[0-9]{2}\1[0-9]{2}\1[0-9]\1[0-9]{3}'
)





share|improve this answer































    0














    Pass pattern as string, without / which are the delimiters for regex



    Validators.pattern('^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/')






    share|improve this answer





















    • 1





      Why do I need a custom validator when there is a built-in pattern validator that should do the job?

      – Anthony
      Jan 29 '18 at 3:58











    • @Anthony I updated my answer, can you give it a shot and let me know, I'n curious if that would solve the issue.

      – Nadhir Falta
      Jan 29 '18 at 4:13











    • With the updated answer I get - SyntaxError: Invalid regular expression: /^^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/$/: Nothing to repeat at new RegExp (<anonymous>)

      – Anthony
      Jan 29 '18 at 5:59











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48493985%2fangular-reactive-forms-pattern-validation-invalid-regular-expression%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    There are multiple issues:




    • The regex literal should not be used inside quotes (or use a string pattern with double escaped special chars)

    • Special chars like (, ) and + must be escaped - nothing to repeat is caused by the fact that ( (start of a capturing group) is quantified with {0,1} and that is an error


    • {0,1} is equal to ?, ( |-) can be written as [ -], (2|4|3|7|8) canbe written shorter as [23478].


    So, you may use



    Validators.pattern(
    '^\(?(0|\+61)[24378]\)?[ -]?[0-9]{2}[ -]?[0-9]{2}[ -]?[0-9][ -]?[0-9]{3}$'
    )


    Note that you may even omit ^ and $ here since the anchors will be added by Angular automatically to the string pattern.



    NOTE: if the separators should be consistent, capture the first one and then use backreferences to the group value:



    Validators.pattern(
    '\(?(?:0|\+61)[24378]\)?([ -]?)[0-9]{2}\1[0-9]{2}\1[0-9]\1[0-9]{3}'
    )





    share|improve this answer




























      1














      There are multiple issues:




      • The regex literal should not be used inside quotes (or use a string pattern with double escaped special chars)

      • Special chars like (, ) and + must be escaped - nothing to repeat is caused by the fact that ( (start of a capturing group) is quantified with {0,1} and that is an error


      • {0,1} is equal to ?, ( |-) can be written as [ -], (2|4|3|7|8) canbe written shorter as [23478].


      So, you may use



      Validators.pattern(
      '^\(?(0|\+61)[24378]\)?[ -]?[0-9]{2}[ -]?[0-9]{2}[ -]?[0-9][ -]?[0-9]{3}$'
      )


      Note that you may even omit ^ and $ here since the anchors will be added by Angular automatically to the string pattern.



      NOTE: if the separators should be consistent, capture the first one and then use backreferences to the group value:



      Validators.pattern(
      '\(?(?:0|\+61)[24378]\)?([ -]?)[0-9]{2}\1[0-9]{2}\1[0-9]\1[0-9]{3}'
      )





      share|improve this answer


























        1












        1








        1







        There are multiple issues:




        • The regex literal should not be used inside quotes (or use a string pattern with double escaped special chars)

        • Special chars like (, ) and + must be escaped - nothing to repeat is caused by the fact that ( (start of a capturing group) is quantified with {0,1} and that is an error


        • {0,1} is equal to ?, ( |-) can be written as [ -], (2|4|3|7|8) canbe written shorter as [23478].


        So, you may use



        Validators.pattern(
        '^\(?(0|\+61)[24378]\)?[ -]?[0-9]{2}[ -]?[0-9]{2}[ -]?[0-9][ -]?[0-9]{3}$'
        )


        Note that you may even omit ^ and $ here since the anchors will be added by Angular automatically to the string pattern.



        NOTE: if the separators should be consistent, capture the first one and then use backreferences to the group value:



        Validators.pattern(
        '\(?(?:0|\+61)[24378]\)?([ -]?)[0-9]{2}\1[0-9]{2}\1[0-9]\1[0-9]{3}'
        )





        share|improve this answer













        There are multiple issues:




        • The regex literal should not be used inside quotes (or use a string pattern with double escaped special chars)

        • Special chars like (, ) and + must be escaped - nothing to repeat is caused by the fact that ( (start of a capturing group) is quantified with {0,1} and that is an error


        • {0,1} is equal to ?, ( |-) can be written as [ -], (2|4|3|7|8) canbe written shorter as [23478].


        So, you may use



        Validators.pattern(
        '^\(?(0|\+61)[24378]\)?[ -]?[0-9]{2}[ -]?[0-9]{2}[ -]?[0-9][ -]?[0-9]{3}$'
        )


        Note that you may even omit ^ and $ here since the anchors will be added by Angular automatically to the string pattern.



        NOTE: if the separators should be consistent, capture the first one and then use backreferences to the group value:



        Validators.pattern(
        '\(?(?:0|\+61)[24378]\)?([ -]?)[0-9]{2}\1[0-9]{2}\1[0-9]\1[0-9]{3}'
        )






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 29 '18 at 8:58









        Wiktor StribiżewWiktor Stribiżew

        318k16139221




        318k16139221

























            0














            Pass pattern as string, without / which are the delimiters for regex



            Validators.pattern('^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/')






            share|improve this answer





















            • 1





              Why do I need a custom validator when there is a built-in pattern validator that should do the job?

              – Anthony
              Jan 29 '18 at 3:58











            • @Anthony I updated my answer, can you give it a shot and let me know, I'n curious if that would solve the issue.

              – Nadhir Falta
              Jan 29 '18 at 4:13











            • With the updated answer I get - SyntaxError: Invalid regular expression: /^^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/$/: Nothing to repeat at new RegExp (<anonymous>)

              – Anthony
              Jan 29 '18 at 5:59
















            0














            Pass pattern as string, without / which are the delimiters for regex



            Validators.pattern('^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/')






            share|improve this answer





















            • 1





              Why do I need a custom validator when there is a built-in pattern validator that should do the job?

              – Anthony
              Jan 29 '18 at 3:58











            • @Anthony I updated my answer, can you give it a shot and let me know, I'n curious if that would solve the issue.

              – Nadhir Falta
              Jan 29 '18 at 4:13











            • With the updated answer I get - SyntaxError: Invalid regular expression: /^^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/$/: Nothing to repeat at new RegExp (<anonymous>)

              – Anthony
              Jan 29 '18 at 5:59














            0












            0








            0







            Pass pattern as string, without / which are the delimiters for regex



            Validators.pattern('^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/')






            share|improve this answer















            Pass pattern as string, without / which are the delimiters for regex



            Validators.pattern('^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/')







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 29 '18 at 4:03

























            answered Jan 29 '18 at 3:57









            Nadhir FaltaNadhir Falta

            982315




            982315








            • 1





              Why do I need a custom validator when there is a built-in pattern validator that should do the job?

              – Anthony
              Jan 29 '18 at 3:58











            • @Anthony I updated my answer, can you give it a shot and let me know, I'n curious if that would solve the issue.

              – Nadhir Falta
              Jan 29 '18 at 4:13











            • With the updated answer I get - SyntaxError: Invalid regular expression: /^^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/$/: Nothing to repeat at new RegExp (<anonymous>)

              – Anthony
              Jan 29 '18 at 5:59














            • 1





              Why do I need a custom validator when there is a built-in pattern validator that should do the job?

              – Anthony
              Jan 29 '18 at 3:58











            • @Anthony I updated my answer, can you give it a shot and let me know, I'n curious if that would solve the issue.

              – Nadhir Falta
              Jan 29 '18 at 4:13











            • With the updated answer I get - SyntaxError: Invalid regular expression: /^^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/$/: Nothing to repeat at new RegExp (<anonymous>)

              – Anthony
              Jan 29 '18 at 5:59








            1




            1





            Why do I need a custom validator when there is a built-in pattern validator that should do the job?

            – Anthony
            Jan 29 '18 at 3:58





            Why do I need a custom validator when there is a built-in pattern validator that should do the job?

            – Anthony
            Jan 29 '18 at 3:58













            @Anthony I updated my answer, can you give it a shot and let me know, I'n curious if that would solve the issue.

            – Nadhir Falta
            Jan 29 '18 at 4:13





            @Anthony I updated my answer, can you give it a shot and let me know, I'n curious if that would solve the issue.

            – Nadhir Falta
            Jan 29 '18 at 4:13













            With the updated answer I get - SyntaxError: Invalid regular expression: /^^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/$/: Nothing to repeat at new RegExp (<anonymous>)

            – Anthony
            Jan 29 '18 at 5:59





            With the updated answer I get - SyntaxError: Invalid regular expression: /^^({0,1}((0|+61)(2|4|3|7|8))){0,1}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{2}( |-){0,1}[0-9]{1}( |-){0,1}[0-9]{3}$/$/: Nothing to repeat at new RegExp (<anonymous>)

            – Anthony
            Jan 29 '18 at 5:59


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48493985%2fangular-reactive-forms-pattern-validation-invalid-regular-expression%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)