Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

437
Visualizações
Cómo agregar http:// si no existe en una cadena que contiene varias URL

Cadena que estoy comprobando:

 Unroll the pastry on to a lightly floured work surface. Cut is [Google](www.google.com/) into 2 long rectangles. Cut the large rectangle in half lengthways, then cut both smaller reckk Bake in the oven for 25–30 minutes, or until the pastry has turned golden-brown and looks crisp. Remove from the oven and leave to cool slightly before serving. Unroll the pastry on to a lightly floured work surface. this is my [web](www.stackoverflow.com), this is [Google](https://www.google.com/) Cut into 2 long rectangles. Cut the large rectangle in half lengthways, then cut both smaller rectangles into eight equal sections. You now have 16 rectangles in total. Brush one end of each rectangle with a little of the beaten egg

Arriba está la cadena desde la que estoy revisando la URL si la URL no tiene http o https. La estoy agregando usando la lógica a continuación, pero el problema aquí es que solo verifique la primera URL y reemplácela si http o https no existe. no se cambian otras URL que no tienen http o https

A continuación se muestra la lógica que había aplicado:

 async urlify(text) { var urlRegex = new RegExp( '([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?' ); return await text.replace(urlRegex, (url) => { if (url.search(/^http[s]?\:\/\//) === -1) { return 'http://' + url; } else { return url; } }); }

¿Puede alguien decirme qué está mal aquí?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Usa el modificador g . Sin ella, su expresión regular solo encontrará la primera coincidencia. Además, String.replace() no devuelve una Promesa. No necesitas await .

 const str = ` Unroll the pastry on to a lightly floured work surface. Cut is [Google](www.google.com/) into 2 long rectangles. Cut the large rectangle in half lengthways, then cut both smaller reckk Bake in the oven for 25–30 minutes, or until the pastry has turned golden-brown and looks crisp. Remove from the oven and leave to cool slightly before serving. Unroll the pastry on to a lightly floured work surface. this is my [web](www.stackoverflow.com), this is [Google](https://www.google.com/) Cut into 2 long rectangles. Cut the large rectangle in half lengthways, then cut both smaller rectangles into eight equal sections. You now have 16 rectangles in total. Brush one end of each rectangle with a little of the beaten egg`; function urlify(text) { var urlRegex = new RegExp( '([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?', 'g' ); return text.replace(urlRegex, (url) => { if (url.search(/^http[s]?\:\/\//) === -1) { return 'http://' + url; } else { return url; } }); } console.log(urlify(str));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda