Question

I need to format the phone numbers found in the source string, preserving the position of the numbers in the source string.

I have an arbitrary string containing list of phone numbers. They are listed irregularly, interspersed by various markers and punctuation, for example:

  • mobile: marker preceding cellphone numbers
  • tel.: marker preceding phones in general. It can be in any version, e. g. Tel:, tel. :, tel., TEL:. It can appear anywhere and in any quantities.
  • ,, ., {space}, {tab} as separators.

In general, "phone number" in the source string is a string conforming to regexp:

/[0-9 +()-]+/

So, I can successfully match on all phones in the source string using the above regexp. However, AFAIK I cannot just do str.replace(regexp, replacement), because the processing of numbers will be done by libphonenumber.

I must preserve all of the noise around the numbers, because it's not always a noise. Or I would just reformatted uniformly the whole string.

I suppose, this question extrapolates to the following: how to map a function over all the substrings found by a regular expression, but nevermind.

Is there any way to do what is described above? Target language is Javascript (well, the kind of Javascript parsable by Adobe Illustrator scripting engine, to be absolutely specific).

Examples

Language is Russian. Enforced format of phones is "international" (how it's called in libphonenumber documentation).


Input: Тел: (343) 378-33-00, моб.: +7(904) 54-73-818

Desired output: Тел: 8 (343) 378-33-00, моб.: 8 (904) 547-38-18

Here is just reformatting of the numbers. Tokens Тел: and , моб.: are left intact.


Input: 8(495) 941 93 20 моб. 8 915-295-62-86

Desired output: 8 (495) 941-93-20 моб. 8 (915) 295-62-86

Here we just reformatted numbers, too. Token моб left intact along with spaces surrounding it.


Input: Тел: 8(495) 685-95-95 доб.:4093, моб.: +7(916) 885-11-58

Desired output: Тел: 8 (495) 685-95-95 доб.:4093, моб.: 8 (916) 885-11-58

Do not care about spaces collapsing or placing punctuation correctly, it'll be done at later stages of processing.


Was it helpful?

Solution

String.replace can take a function as the second argument, in which case the function is called for every match and the return value is what the match is replaced with.

The function arguments may include the match or other things, based on their names. You can have a look at the docs for replace for more details.

In any case, this seems to do the trick:

inputString = "Тел: (343) 378-33-00, моб.: +7(904) 54-73-818";

function replacer(match) {
    // you would call libphonenumber here and return the formatted number
    return "{" + match + "}";
};

outputString = inputString.replace(/[\-\+0-9 \(\)]+/g, replacer);

P.S. There is also a small problem with the regex you have which matches a phone number. It will for example match just " ", or just "-". A better regex which avoids this may be something like:

/[\-\+\(]*[0-9][\-\+ \(\)0-9]*/g

This may take some tuning.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top