Replacing wildcard text using jquery

2019-02-24 18:21发布

问题:

I have a database that contains company information (address, telephone etc)

On some of the telephone numbers it will have an international code: +44 (0) 123 12345

where the (0) will be different numbers depending on country.

I need to strip the (0)

I have the following code:

var el = $('#contactdetails');
el.html(el.html().replace("(0)", "-"));

which works on (0) - but how do I do this for wildcards

回答1:

Use a regular expression.

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]*\)/, "-"));

Live example here