Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 651
Tirthankar Kundu Avatar asked May 28 '14 05:05

Tirthankar Kundu


2 Answers

This will do your job:

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

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

alert(result);
like image 96
Sankalp Bhatt Avatar answered Sep 19 '22 15:09

Sankalp Bhatt


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.

like image 28
rdleal Avatar answered Sep 18 '22 15:09

rdleal