Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

320
Views
Adding a multiple space character's in nodejs using replace method

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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Just use the space character:

var s = 'Hi Al !'
var new_var = s.replace('Hi', 'Hi    ');
console.log(new_var)

about 4 years ago · Juan Pablo Isaza Report

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!