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

230
Views
Sustituye todas las letras excepto la primera en cada palabra
text = "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,"; substitute_with = "_"; const regex = '\B[A-Za-z]'; // const regex = '\B\w'; // const regex = '(\w{1})\w*'; var result = text.replaceAll(regex, substitute_with);

Me gustaría sustituir con guión bajo todas las letras excepto la primera de cada palabra.

¿Qué puedo probar a continuación?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Use la bandera g (global) con replace() :

 const text = "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,"; const substitute_with = "_"; const regex = /\B[A-Za-z]/g; var result = text.replace(regex, substitute_with); console.log(result);

L____ I____ h__ b___ t__ i____ de s_______ d____ t___ e___ s____ t__ 1500_,


Por supuesto, esto también se puede hacer sin expresiones regulares usando split() , substring() , repeat() y join() :

 const result = text.split(' ') .map(w => w.substring(0, 1) + substitute_with.repeat(w.length - 1)) .join(' ');
about 4 years ago · Santiago Trujillo Report

0

Puede hacer coincidir todas las palabras primero, luego separar la cabeza y la cola de la palabra y reemplazar todas las letras en la parte de la cola.

 const text = "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,"; const result = text.replace( /\b\S+\b/gi, // For each word (g0) => g0.replace( /\b([az])(.+)\b/i, // Grab the head and tail (_g0, _g1, _g2) => `${_g1}${_g2.replace(/[az]/gi, '_')}` // Keep head, replace all others with '_' ) ); console.log(text); console.log(result);
 .as-console-wrapper { top: 0; max-height: 100% !important; }

about 4 years ago · Santiago Trujillo 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!