使用jQuery替换通配符文本
问题描述:
我有一个包含公司信息(地址,电话等)的数据库
I have a database that contains company information (address, telephone etc)
在某些电话号码上,将使用国际代码:+44(0)123 12345
On some of the telephone numbers it will have an international code: +44 (0) 123 12345
其中(0)取决于国家/地区的数字将不同.
where the (0) will be different numbers depending on country.
我需要剥离(0)
我有以下代码:
var el = $('#contactdetails');
el.html(el.html().replace("(0)", "-"));
可在(0)上使用-但是我该如何对通配符进行操作
which works on (0) - but how do I do this for wildcards
答
使用正则表达式.
var el = $('#contactdetails');
el.html(el.html().replace(/\([0-9]\)/, "-"));
如果有一个以上的单数位,则将*
用于上一个表达式的任意出现次数.
If there is more than a singe digit, then use the *
for any number of occurrences of the previous expression.
el.html(el.html().replace(/\([0-9]*\)/, "-"));