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

69
Views
Regex pattern to replace a local address with a hyperlink address in javascript

I have a string

var str = "C:\Program Files\nodejs\node_modules"

I want to replace the "C:\Program Files\" part with a hyperlink part "https://" so that the final outcome is

"https://nodejs/node_modules"

The first step to replace the slases with regex I already did it with

replace(/\\\\/gi,"/")

hence it became

"C:/Program Files/nodejs/node_modules"

So, the regex for the final outcome ,am facing difficulties.

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

0

You might use 2 capture groups:

^[A-Z]+:\\(?:[^\\\n]*\\)*([^\\\n]+)\\([^\\\n]+)$
  • ^ Start of string
  • [A-Z]+:\\ Match 1+ chars A-Z : and \
  • (?:[^\\\n]*\\)* Optional repetitions of any char except \ ad then match \
  • ([^\\\n]+) Capture group 1 Match optional chars other than \
  • \\ Match \
  • ([^\\\n]+) Capture group 2 the same as for capture group 1
  • $ End of string

And replace with

https://$1/$2

See a regex demo.

const regex = /^[A-Z]:\\(?:[^\\\n]*\\)*([^\\\n]+)\\([^\\\n]+)$/gm;
const str = `C:\\Program Files\\nodejs\\node_modules`;
const result = str.replace(regex, `https://$1/$2`);
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Find

^[A-Z]:\/[^\/]+(.*)

Replace

https:/$1
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!