Skip to content

International phone numbers support #25

Closed
@joaopiopedreira

Description

@joaopiopedreira

Hi @joshswan , it looks like we can override the phone regex directly in your src/matchers.js file. I've done it in my branch and it works like a charm. The idea is to add a regex by country and then merge them all when invoking the phone autolinker.

To add a new country, we would add the corresponding regex to this object (in src/matchers.js):

// To include a new country, find it's ISO Alpha 2 code at http://www.nationsonline.org/oneworld/country_code_list.htm
const regexByCountry = {
  US: /(\([0-9]{3}\)[\s-]|[0-9]{3}[\s-])[0-9]{3}[-\s][0-9]{4}/g,
  GB: /(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)?(44)\)?[\s-]?(? (...),
  FR: /(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)?(44)\)?[\s-]?(? (...),
  PT: /(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)?(44)\)?[\s-]?(? (...),
  IE:  /(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)?(44)\)?[\s-]?(? (...),
  DE:  /(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)?(44)\)?[\s-]?(? (...),
};

Our phone override would then look like this:

  // Phone override
  {
    id: 'phone',
    regex: combinedRegex(['US', 'GB', 'FR', 'PT', 'IE', 'DE']),
    Match: Autolinker.Util.extend(Autolinker.match.Match, {
      constructor(cfg) {
        Autolinker.match.Match.prototype.constructor.call(this, cfg);
        this.number = cfg.phone;
      },
      getType() {
        return 'phone';
      },
      getNumber() {
        return this.number;
      },
      getAnchorHref() {
        return `tel:${this.number}`;
      },
      getAnchorText() {
        return this.matchedText;
      },
    }),
  },

Is this something you'd want to include in your package? Should I submit a PR? Cheers

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions