Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

114
Vistas
node - How to replace part of an url

I have an array of urls like this:

[
 'https://subdomain1.example.com/foo-bar/', 
 'https://subdomain2.example.com/foo-bar',
 'https://subdomain2.example.com/foo-bar',
 'https://subdomain2.example.com/foo-bar',
 'https://subdomain2.example.com/foo-bar'
]

I need to search inside it to match the user input with the subdomain part of the url, I'm trying with thism line of code to achive it:

 const searched = urlList.find( el => el.includes( match[1].toLocaleLowerCase() ) )
 console.log(searched.length)

If the input is find, I need to replace the second part of the url, in my case /foo-bar with /foo-baz or /foo-baz-bar to obtain a response for the user that is something like https://subdomain2.example.com/foo-bar-baz/.

At the moment I'm not sure how to proceed, is there any function in JS that can help me?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can simply achieve that with a single line of code.

const urlList = [
 'https://subdomain1.example.com/foo-bar/', 
 'https://subdomain2.example.com/foo-bar',
 'https://subdomain2.example.com/foo-bar',
 'https://subdomain2.example.com/foo-bar',
 'https://subdomain2.example.com/foo-bar'
];

const findSubString = 'foo-bar';
const replaceSubString = 'foo-baz';

const res = urlList.map((url) => url.replace(findSubString, replaceSubString));

console.log(res);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Finding domain and replace part of URL with regex

function foo() {
  let a = ['https://subdomain1.example.com/foo-bar/', 'https://subdomain2.example.com/foo-bar', 'https://subdomain2.example.com/foo-bar', 'https://subdomain2.example.com/foo-bar', 'https://subdomain3.example.com/foo-bar'];
  let seed = 'subdomain2.example.com';
  let repl = 'whatever';

  for (let i = 0; i < a.length; i++) {
    let e = a[i];
    let r = e.match(/\/\/([^\/]+)/);
    if (r[1] == seed) {
      a[i] = e.replace(/(?<=\.\w{3}\/)[a-zA-Z0-9_-]+/, repl);
    }
  }
  //document.getElementById("jj").value = JSON.stringify(a);
  console.log(JSON.stringify(a));
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

I've found a simple solution and solved the problem Here is my snippet for everyone that in future that need the same thing

    const searched = urlList.find( el => el.includes( match[1].toLocaleLowerCase() ) )
    const url = new URL(searched)
    const host = url.host
    const responseURL = `https://www.${host}${responsePath}`
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda