Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to linkify unicode numbers in javascript using regex

How can I change the regex below to also select the unicode numbers? Currently only ASCII numbers are selected.

function numberfy(text) {
    var urlRegex = /[+0-9]+(?:\.[0-9]*)?[0-9]{5,}/g;

    return text.replace(urlRegex, function(url) {
        return '<font color="blue"><u><a href="tel:' + url + '">' + url + '</a></u></font>';
    });
}

Thanks

like image 703
skcrpk Avatar asked Nov 29 '25 03:11

skcrpk


1 Answers

Your question is unclear, but if you are looking to include alternate Unicode numeral forms, such as the Unicode full-width characters, you can add in explicit Unicode ranges like this:

// Adds the full-width unicode range FF10-FF19 (    0-9)
var urlRegex = /[+0-9\uFF10-\uFF19]+(?:\.[0-9\uFF10-\uFF19]*)?[0-9\uFF10-\uFF19]{5,}/g;

A working example. You can add additional ranges simply by tacking them on. I'd be tempted to modify your code so you could reduce the duplication if you have more than a few ranges:

var digit = "0-9\uFF10-\uFF19";
var urlRegex = new RegExp("[+"+digit+"]+(?:\\.["+digit+"]*)?["+digit+"]{5,}", "g");

A list of alternate Unicode numeric forms can be found here. This includes other forms in other languages.

Please note that only 2-byte Unicode values will work (up to \uFFFF). On that page they include some extended forms (Mathematical Bold, for example) that are outside the 2-byte Unicode range supported by JavaScript.

like image 155
OverZealous Avatar answered Nov 30 '25 17:11

OverZealous



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!