1 | import { __extends } from "tslib";
|
---|
2 | import { Matcher } from './matcher';
|
---|
3 | import { PhoneMatch } from '../match/phone-match';
|
---|
4 | import { nonDigitRe } from '../regex-lib';
|
---|
5 | // RegExp objects which are shared by all instances of PhoneMatcher. These are
|
---|
6 | // here to avoid re-instantiating the RegExp objects if `Autolinker.link()` is
|
---|
7 | // called multiple times, thus instantiating PhoneMatcher and its RegExp
|
---|
8 | // objects each time (which is very expensive - see https://github.com/gregjacobs/Autolinker.js/issues/314).
|
---|
9 | // See descriptions of the properties where they are used for details about them
|
---|
10 | // Over the years, many people have added to this regex, but it should have been
|
---|
11 | // split up by country. Maybe one day we can break this down.
|
---|
12 | var mostPhoneNumbers = /(?:(?:(?:(\+)?\d{1,3}[-\040.]?)?\(?\d{3}\)?[-\040.]?\d{3}[-\040.]?\d{4})|(?:(\+)(?:9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)[-\040.]?(?:\d[-\040.]?){6,12}\d+))([,;]+[0-9]+#?)*/;
|
---|
13 | // Regex for Japanese phone numbers
|
---|
14 | var japanesePhoneRe = /(0([1-9]{1}-?[1-9]\d{3}|[1-9]{2}-?\d{3}|[1-9]{2}\d{1}-?\d{2}|[1-9]{2}\d{2}-?\d{1})-?\d{4}|0[789]0-?\d{4}-?\d{4}|050-?\d{4}-?\d{4})/;
|
---|
15 | // Combined regex
|
---|
16 | var phoneMatcherRegex = new RegExp("".concat(mostPhoneNumbers.source, "|").concat(japanesePhoneRe.source), 'g');
|
---|
17 | /**
|
---|
18 | * @class Autolinker.matcher.Phone
|
---|
19 | * @extends Autolinker.matcher.Matcher
|
---|
20 | *
|
---|
21 | * Matcher to find Phone number matches in an input string.
|
---|
22 | *
|
---|
23 | * See this class's superclass ({@link Autolinker.matcher.Matcher}) for more
|
---|
24 | * details.
|
---|
25 | */
|
---|
26 | var PhoneMatcher = /** @class */ (function (_super) {
|
---|
27 | __extends(PhoneMatcher, _super);
|
---|
28 | function PhoneMatcher() {
|
---|
29 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
---|
30 | /**
|
---|
31 | * The regular expression to match Phone numbers. Example matches:
|
---|
32 | *
|
---|
33 | * (123) 456-7890
|
---|
34 | * 123 456 7890
|
---|
35 | * 123-456-7890
|
---|
36 | * +18004441234,,;,10226420346#
|
---|
37 | * +1 (800) 444 1234
|
---|
38 | * 10226420346#
|
---|
39 | * 1-800-444-1234,1022,64,20346#
|
---|
40 | *
|
---|
41 | * This regular expression has the following capturing groups:
|
---|
42 | *
|
---|
43 | * 1 or 2. The prefixed '+' sign, if there is one.
|
---|
44 | *
|
---|
45 | * @protected
|
---|
46 | * @property {RegExp} matcherRegex
|
---|
47 | */
|
---|
48 | _this.matcherRegex = phoneMatcherRegex;
|
---|
49 | return _this;
|
---|
50 | }
|
---|
51 | /**
|
---|
52 | * @inheritdoc
|
---|
53 | */
|
---|
54 | PhoneMatcher.prototype.parseMatches = function (text) {
|
---|
55 | var matcherRegex = this.matcherRegex, tagBuilder = this.tagBuilder, matches = [], match;
|
---|
56 | while ((match = matcherRegex.exec(text)) !== null) {
|
---|
57 | // Remove non-numeric values from phone number string
|
---|
58 | var matchedText = match[0], cleanNumber = matchedText.replace(/[^0-9,;#]/g, ''), // strip out non-digit characters exclude comma semicolon and #
|
---|
59 | plusSign = !!(match[1] || match[2]), // match[ 1 ] or match[ 2 ] is the prefixed plus sign, if there is one
|
---|
60 | before = match.index == 0 ? '' : text.substr(match.index - 1, 1), after = text.substr(match.index + matchedText.length, 1), contextClear = !before.match(/\d/) && !after.match(/\d/);
|
---|
61 | if (this.testMatch(match[3]) && this.testMatch(matchedText) && contextClear) {
|
---|
62 | matches.push(new PhoneMatch({
|
---|
63 | tagBuilder: tagBuilder,
|
---|
64 | matchedText: matchedText,
|
---|
65 | offset: match.index,
|
---|
66 | number: cleanNumber,
|
---|
67 | plusSign: plusSign,
|
---|
68 | }));
|
---|
69 | }
|
---|
70 | }
|
---|
71 | return matches;
|
---|
72 | };
|
---|
73 | PhoneMatcher.prototype.testMatch = function (text) {
|
---|
74 | return nonDigitRe.test(text);
|
---|
75 | };
|
---|
76 | return PhoneMatcher;
|
---|
77 | }(Matcher));
|
---|
78 | export { PhoneMatcher };
|
---|
79 | //# sourceMappingURL=phone-matcher.js.map |
---|