Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Don't use a regular expression to simply remove a <code>+1</code> from the beginning of a string; it is overkill. In this case, it is fine to say:</p> <pre><code>if (phoneNumber &amp;&amp; phoneNumber.indexOf('+1') === 0) { phoneNumber = phoneNumber.substring(2); } </code></pre> <p>However, the <em>general</em> solution to the problem of stripping country codes would have to consider all known country codes, which exist in several places, including <a href="http://en.wikipedia.org/wiki/List_of_country_calling_codes" rel="nofollow noreferrer">Wikipedia</a>. It will need a few lines of code, though. You have to take into account things like:</p> <ul> <li>If your string starts with <code>+1</code> then the country code is 1.</li> <li>If your string starts with a <code>+2</code>, then if the next character is <code>0</code> or <code>7</code>, you have a two-digit country code, otherwise if the second character is an <code>8</code> then you have an error, otherwise you have a 3-digit code.</li> </ul> <p>There are similar rules for the first digit after the <code>+</code> being a 3 through 9.</p> <p>At this point, a regex becomes an interesting, although wild-looking solution, it has the following form</p> <pre><code>^\+[1|2[07]|2[1-69]\d| ..the rest of it goes here... ] </code></pre> <p>A related SO question: <a href="https://stackoverflow.com/questions/5777375/regular-expression-regex-for-phone-numbers-with-country-code-check">Regular Expression (RegEx) For Phone Numbers With Country Code Check</a></p> <p><strong>EDIT</strong></p> <p>One of the things that makes the general problem difficult is that the list of country codes can expand. Right now, there are no country codes starting with 28, but that group is reserved for future expansion, so your code would have to change if new codes are assigned. And it's true that the full regex would be quite long, hard to read, and error prone. Some kind of table-driven approach with a function would IMHO be the nicest way to go, but unless you are in the telecommunications business I'm thinking you can write an app which keeps the calling code distinct from the number. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload