colymore
colymore

Reputation: 12316

Replace All letters and more than 2 spaces in javascript string for empty character

i need to replace in javascript string all letters and more than 2 spaces for "". I have this:

order.Order.Telefonos = order.Order.Telefonos.replace(/[^A-Za-z]|[^\S\r\n]{2,}/, '');

But when order.Order.Telefonos have this:

"CASA: 1111111111      Varios:                Trabajo: 111111111"

return me this:

1111111111Varios:Trabajo:111111111

whats wrong on my regex?

Upvotes: 1

Views: 115

Answers (2)

Kijewski
Kijewski

Reputation: 26022

You did not copy the result verbatim, for your input it would be

'CASA 1111111111      Varios:                Trabajo: 111111111'

Then to your actual question: [^A-Za-z] is everything but letters, omit the ^. Then do a global search with /…/g to find and replace every instance.

Upvotes: 1

falsetru
falsetru

Reputation: 369074

You're using negation: [^...]. Just use character class without negation, and use global modifier (/..../g) to replace all matches.

var s = "CASA: 1111111111      Varios:                Trabajo: 111111111";
s.replace(/[A-Za-z]|\s{2,}/g, '')
// => ": 1111111111:: 111111111"

Upvotes: 2

Related Questions