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

195
Views
How do I move nested leading spaces with regex/javascript?

Can/how do you do a multipart regex replace that looks for content within some tags then moves any space padding outside of the tags?

sample start string:

random text to ignore** some text **other text to ignore** other important text** blah blah blah

result:

random text to ignore **some text** other text to ignore **other important text** blah blah blah

basically trying to help a markdown to html library parse correctly where it is having trouble with spaces right after MD age so i want to do a quick scrub first. The conversion will be done in python with markdown2 or in javascript with showdown.js.

I am hoping for a syntax like s.replace(regex, '$1**$2**$3') where 1 and 3 would be spaces within the tags, if they exist

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

0

You can use

text.replace(/\*{2}(\s*)(.*?)(\s*)\*{2}/g, '$1**$2**$3')

See the regex demo. Details:

  • \*{2} - a ** string
  • (\s*) - Group 1 ($1): zero or more whitespaces
  • (.*?) - Group 2: any zero or more chars other than line break chars as few as possible (use [^]*? or [\w\W]*? to match across line breaks)
  • (\s*) - Group 3 ($3): zero or more whitespaces
  • \*{2} - a ** string

See a JavaScript demo:

const text = "random text to ignore** some text **other text to ignore** other important text** blah blah blah";
console.log(text.replace(/\*{2}(\s*)(.*?)(\s*)\*{2}/g, '$1**$2**$3'));

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!