Hightlight numbers in phone number

你。 提交于 2019-12-24 17:09:54

问题


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/


回答1:


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.

Demo.



来源:https://stackoverflow.com/questions/7916659/hightlight-numbers-in-phone-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!