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
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
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, ""));