Tirthankar Kundu
Tirthankar Kundu

Reputation: 705

Removing spaces, hyphen and brackets from a string

I am getting a string like:

 var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';

I want to remove the spaces, hyphen and brackets from every number. Something like:

var str = '+911234567891,432123234,12312313456,4325671234';

Please suggest a way to achieve this.

Upvotes: 10

Views: 16806

Answers (2)

Sankalp Bhatt
Sankalp Bhatt

Reputation: 1204

This will do your job:

var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';

var result = str.replace(/[- )(]/g,'');

alert(result);

Upvotes: 33

rdleal
rdleal

Reputation: 1032

You can use Regular Expression to replace those items by empty string:

'+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)'.replace(/[\s()-]+/gi, '');
// results in "+911234567891,432123234,12312313456,4325671234"

Hope it helps.

Upvotes: 8

Related Questions