简体   繁体   中英

Search and replace a set of numbers

I am using JavaScript and Regex to search for a known phone number in a given piece of text and then replace it with some other number. Although I know the phone number but I am not certain of the phone number format used in the given text. The text could be in any language.

For instance, I need to find the phone number +14101111111 and replace it with +14102222222 in the following string:

Call me at +1-(410)-111-1111.

Preferably I want to replace the old number with the new number keeping the same format as the old number. So the result string should look like:

Call me at +1-(410)-222-2222.

I am using the following regex for searching the number in the string. I split up the phone number and apply the regex after every single number.

([&;nbsp_\.\(\)\+\-~\*•–\s]*)

So the full regex, in this case, will look like:

/1([&;nbsp_\.\(\)\+\-~\*•–\s]*)4([&;nbsp_\.\(\)\+\-~\*•–\s]*)1([&;nbsp_\.\(\)\+\-~\*•–\s]*)0([&;nbsp_\.\(\)\+\-~\*•–\s]*)1([&;nbsp_\.\(\)\+\-~\*•–\s]*)1([&;nbsp_\.\(\)\+\-~\*•–\s]*)1([&;nbsp_\.\(\)\+\-~\*•–\s]*)1([&;nbsp_\.\(\)\+\-~\*•–\s]*)1([&;nbsp_\.\(\)\+\-~\*•–\s]*)1([&;nbsp_\.\(\)\+\-~\*•–\s]*)1/g

This does match the number fine and I can capture the groups.

I will use this method on webpages.

Question: What are the caveats of using this method? Can you think of a better approach? How well will this work with multi-lingual webpages?

Thanks!

for maintainablity's sake, i'd shoot for constructing your regex programatically:

var originalNumber = '14101111111';
var potentialDelimiters = '(( |[_\\.\\(\\)\\+\\-~\\*•–\\s])*)';

var regexString = originalNumber.split('').join(potentialDelimiters);
var numberSwapper = new RegExp(regexString);

my two cents: It sounds like you found a working solution to a fairly awkward issue; optimization can sometimes take up more time than it saves. Run with what you've got — just keep it maintainable in the event you need to optimize in the future. good luck!

I'd avoid the "nbsp" characters inside the character set, and use a separate match for that, unless you want to capture "+1nnnnnsnssb410111nsns111bnsnsnss1".

I'd also consider changing the * to a more limited count of characters, maybe {1,5}

Change it to (( ?|[_\\.\\(\\)\\+\\-~\\*•–\\s]){1,5})

Maybe you could try looping through the text looking for numbers in a row with special characters in the middle, since you know the number your just have to check if they are in the correct order and if the characters in the middle of them are characters usually used to display phone numbers. If the condition is true you just replace the numbers and let the special characters like they originally were used.

尝试使用:

string.replace( /\d+(\D?)\d+([^\d)-])/g, '222$1222$2')

This is one way I thought you could do it:

 function formatNumber(string, number) { var regExp = /[^\\d\\s\\+]?(\\+?\\d+)[^\\d\\s]?/g; var index = 0; var stringCopy = string.slice(); var arr; while(arr = regExp.exec(string)){ var numReplace = arr[1]; var toReplace = number.substr(index, arr[1].length); index += arr[1].length; stringCopy = stringCopy.replace(arr[1], toReplace); } return stringCopy; //console.log(stringCopy) } function findNumber(number, string){ var regExp = /[^\\d\\s\\+]?(\\+?\\d+)[^\\d\\s]?/g; var index = 0, arr, results = ''; while(arr = regExp.exec(string)){ if(index === number.length){ break; //Found a Possible Match } var numFoundLength = arr[1].length; var numToFind = number.substr(index, numFoundLength); index += numFoundLength; if(numToFind == arr[1]){ results += arr[0]; } else { //Number doesn't follow pattern, reset index = 0; } } if(index !== number.length) { return false; } else { return results; } } function findAndReplaceNumber(numToFind, numToReplace, string){ var numFound = findNumber(numToFind, string); var fNum = formatNumber(numFound, numToReplace); return string.replace(numFound, fNum); } document.getElementById('results').innerHTML = 'Test string = "Call me at +1-(410)-111-1111"\\n'; document.getElementById('results').innerHTML += 'Replace number: +14101111111 for +1410222222"\\n'; document.getElementById('results').innerHTML += findAndReplaceNumber('+14101111111','+14102222222', 'Call me at +1-(410)-111-1111.'); 
 <pre id="results"></pre> 

The way it does it, is looking for numbers that can be surrounded by delimiters and compares the matched number against its current index. It then increases the index by the length of the matched number. I feel this function is most ugly but does the job, a better function would play around with a better expression that looks for the exact number with possible delimiters. This also only looks for one format when there could be more. The formatNumber does a similar thing.

It should be noted that it might behave weird with numbers of different length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM