Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6884263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:29:08+00:00 2026-05-27T05:29:08+00:00

Regular expressions aren’t exactly my strong suit. I got a regex for validating international

  • 0

Regular expressions aren’t exactly my strong suit. I got a regex for validating international phone numbers here. The validation bit works for me but I don’t understand how I can take the regex result and use it to format the number. My question is how do I figure out, from the regex, what the groupings are that I can use to display?

var intl1RegexObj = /^((\+)?[1-9]{1,2})?([-\s\.])?((\(\d{1,4}\))|\d{1,4})(([-\s\.])?[0-9]{1,12}){1,2}$/;


if (IntlRegexObj.test(businessPhoneValue)) 
{
    var formattedPhoneNumber = businessPhoneValue.replace(IntlRegexObj, "($1)");

    // display formatted result
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:29:09+00:00Added an answer on May 27, 2026 at 5:29 am

    After simplifying that mess of a regex:

    if (subject.match(/^((?:\+)?[1-9]{1,2})?[\-\s.]?((?:\(\d{1,4}\))|\d{1,4})([\-\s.]?\d{1,12}){1,2}$/)) {
        // Successful match
    }
    

    There are now only 3 capturing groups.

    First one $1 is easy, the country code with an optional +.

    Then you have the local area code, basically 1-4 numbers with / without parentheses optionally prefixed by [-\s.]. That’s $2

    Finally you have your the actual phone number which can be from 1 to 24 numbers, including optional space or dot or minus sign [-\s.]

    More detailed explanation:

    "
    ^              # Assert position at the beginning of the string
    (              # Match the regular expression below and capture its match into backreference number 1
       (?:         # Match the regular expression below
          \+       # Match the character “+” literally
       )?          # Between zero and one times, as many times as possible, giving back as needed (greedy)
       [1-9]       # Match a single character in the range between “1” and “9”
          {1,2}    # Between one and 2 times, as many times as possible, giving back as needed (greedy)
    )?             # Between zero and one times, as many times as possible, giving back as needed (greedy)
    [-\s.]         # Match a single character present in the list below
                   # The character “-”
                   # A whitespace character (spaces, tabs, line breaks, etc.)
                   # The character “.”
       ?           # Between zero and one times, as many times as possible, giving back as needed (greedy)
    (              # Match the regular expression below and capture its match into backreference number 2
                   # Match either the regular expression below (attempting the next alternative only if this one fails)
          (?:      # Match the regular expression below
             \(    # Match the character “(” literally
             \d    # Match a single digit 0..9
              {1,4}# Between one and 4 times, as many times as possible, giving back as needed (greedy)
             \)    # Match the character “)” literally
          )
       |           # Or match regular expression number 2 below (the entire group fails if this one fails to match)
          \d       # Match a single digit 0..9
             {1,4} # Between one and 4 times, as many times as possible, giving back as needed (greedy)
    )
    (              # Match the regular expression below and capture its match into backreference number 3
       [-\s.]      # Match a single character present in the list below
                   # The character “-”
                   # A whitespace character (spaces, tabs, line breaks, etc.)
                   # The character “.”
          ?        # Between zero and one times, as many times as possible, giving back as needed (greedy)
       \d          # Match a single digit 0..9
          {1,12}   # Between one and 12 times, as many times as possible, giving back as needed (greedy)
    ){1,2}         # Between one and 2 times, as many times as possible, giving back as needed (greedy)
    $              # Assert position at the end of the string (or before the line break at the end of the string, if any)
    "
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can regular expressions be used to perform arithmetic? Such as find all numbers in
Regular expressions are great for text editing. But sometimes they aren't quite enough. For
I've already tried my best but regular expressions aren't really my thing. :( I
Ok, considering the regular expressions aren't working properly and there are no errors, I'm
Regular expressions have never been one of my strong points, and this one has
I've been struggling with this all day, as regular expressions aren’t my most favourite
what regular expressions are used for email validation can you help me out $(Textbox).rules(add,
Regular Expressions are usually expressed as strings, but they also have properties (ie. single
Regular expressions can become quite complex. The lack of white space makes them difficult
Because regular expressions scare me, I'm trying to find a way to remove all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.