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

125
Views
Función que reemplaza la palabra envuelta con ampersands con un solo hashtag al inicio

Traté de escribir una función para reemplazar los símbolos de unión que están alrededor de la palabra en cadena con un solo hashtag que estará al comienzo de la palabra &word& => #word . Y lo hice, pero después de mirar mi código puedo decir que es un poco feo, y también necesito una función que devuelva el texto a la versión con ampersand también. Entonces, tal vez alguien pueda decirme cómo puedo mejorarlo. ¿Quizás alguien pueda proporcionar una versión con expresiones regulares?

 const text = "&string& someText&string2& &string3& &string4&&string5&"; const replaceAmpersandsWithHashtag = (text: string) => { const firstStep = text.replace(/\&/g, '#'); let occurenceOfHashtag = 0; const secondStep = firstStep.split('').filter(char => { if(char === '#') { occurenceOfHashtag += 1 ; return occurenceOfHashtag % 2 === 0 ? false : true; } return true }).join(''); return secondStep } const replaceHashtagWithAmpersands = (text: string) => { const firstStep = text.replace(/\#/g, '&'); let isLookingForNextAmpersand = false; const secondStep = firstStep.split('').map((char, index) => { if(char === '&') { isLookingForNextAmpersand = true; return char; } if(isLookingForNextAmpersand && firstStep.length === index + 1) { isLookingForNextAmpersand = false; return `${char}&` } if(isLookingForNextAmpersand && (firstStep.charAt(index + 1) === ' ' || firstStep.charAt(index + 1) === '&')) { isLookingForNextAmpersand = false; return `${char}&` } return char }).join(''); return secondStep } const textWithHastags = replaceAmpersandsWithHashtag(text); const againWithAmpersands = replaceHashtagWithAmpersands(textWithHastags); // Should be "#string someText#string2 #string3 #string4#string5" console.log(textWithHastags) // Should be "&string& someText&string2& &string3& &string4&&string5&" console.log(againWithAmpersands) // Should be "true" console.log(text === againWithAmpersands)
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Utilice un grupo de captura en la expresión regular para copiar la palabra entre & en el reemplazo.

 function replaceAmpersandsWithHashtag(string) { return string.replace(/&(\w+)&/g, '#$1'); } function replaceHashtagWithAmpersands(string) { return string.replace(/#(\w+)/g, '&$1&'); } const text = "&string& someText&string2& &string3& &string4&&string5&"; const textWithHastags = replaceAmpersandsWithHashtag(text); const againWithAmpersands = replaceHashtagWithAmpersands(textWithHastags); // Should be "#string someText#string2 #string3 #string4#string5" console.log(textWithHastags) // Should be "&string& someText&string2& &string3& &string4&&string5&" console.log(againWithAmpersands) // Should be "true" console.log(text === againWithAmpersands)

about 4 years ago · Juan Pablo Isaza Report

0

está exagerando la solución, puede hacerlo fácilmente con la función replaceAll que reemplazará las ocurrencias de su búsqueda, en este caso necesitará usar algunas regex para hacer un reemplazo adecuado, por lo que puede usar &(\w+)& donde la parte (\w+) capturará el grupo entre & y luego puede usarlo en la llamada de reemplazo como $1 .

aquí está su código con la implementación de la explicación anterior

 const text = "&string& someText&string2& &string3& &string4&&string5&"; const replaceAmpersandsWithHashtag = (text) => { return text.replaceAll(/&(\w+)&/g, '#$1'); } const replaceHashtagWithAmpersands = (text) => { return text.replaceAll(/#(\w+)/g, '&$1&'); } const textWithHastags = replaceAmpersandsWithHashtag(text); const againWithAmpersands = replaceHashtagWithAmpersands(textWithHastags); // Should be "#string someText#string2 #string3 #string4#string5" console.log(textWithHastags) // Should be "&string& someText&string2& &string3& &string4&&string5&" console.log(againWithAmpersands) // Should be "true" console.log(text === againWithAmpersands)

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!