Take the 2-minute tour ×

This function hightlight numbers.

var hightlightPhoneNumbers = function(phoneNumber, hNumbers) {
  var splitPhoneNumber = phoneNumber.split(hNumbers);
  var result = '';

  if (splitPhoneNumber.length > 1) {
    for (var i = 0; i < splitPhoneNumber.length - 1; i++) {
      result += splitPhoneNumber[i] + '<b>' + hNumbers + '</b>';
    return result + splitPhoneNumber[splitPhoneNumber.length - 1];
  return phoneNumber;

And we can usage it as hightlightPhoneNumbers('+123456789','123').

But we have a little problem. What if phone number looks like +1 234-567-89? Ofcourse function doesnt work correct. How we can to fix it?

Hightlight means wrap numbers in tags or something. see http://jsfiddle.net/QhJWt/3/

share|improve this question
Abstract this. How can you highlight a substring? The fact it's a phone number doesn't matter. A more general solution exists. –  Incognito Oct 27 '11 at 13:41
What is the function supposed to do? What does "highlighting numbers" mean? –  Alex Turpin Oct 27 '11 at 13:41
add comment

1 Answer

up vote 2 down vote accepted
function hightlightPhoneNumbers(phoneNumber, clip) {
    var re = new RegExp(clip.split('').join('[\\s-]*'), 'gi');
    return phoneNumber.replace(re, '<b>$&</b>');

By the way, there is this funny non-standard method of String.prototype.bold that wraps a string into a <b></b> tag.


share|improve this answer
Works great. Thanks! –  Neir0 Oct 27 '11 at 14:07
add comment

Your Answer


