-1

I'm trying to delete the white space form string using js, I found a solution that deletes ALL the white space .replace(/\s/g,""), but I need to keep the space between the string.

Ex: ****example*1**** -> example*1

* space

MahmouD Skafi
  • 370
  • 3
  • 15

1 Answers1

1

You should use trim() to remove spaces from start and end. But if you still want to use regex or your browser doesn't support trim natively then you can do this way,

str = '    example 1    ';
console.log(str.replace(/^\s+|\s+$/g, ""));
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103