I have the following variable.
var s = 'Hi Al !'
Which i would like to convert to
'Hi Al !'
I.E. basically add 4 space characters and hence taught i could do the following:-
var new_var = s.replace('Hi', 'Hi\s\s\s\s');
But the above only gives me the output Hissss Al !
, which is not what i want. How do i add 4 space characters to my string ?
I have also tried var new_var = s.replace('Hi', 'Hi\t');
But the output has about 6-7 spaces.
CodePudding user response:
Just use the space character:
var s = 'Hi Al !'
var new_var = s.replace('Hi', 'Hi ');
console.log(new_var)
CodePudding user response:
Using \s
matches a whitespace char using a regular expression. If you use that in the replacement, it will just be s
If there is a variable number of spaces after Hi, and you want to change that to always have 4 spaces, you can match 1 or more whitespace chars and use 4 spaces in the replacement.
var s = 'Hi Al !';
var new_var = s.replace(/Hi\s /, 'Hi ');
console.log(new_var)