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

140
Views
Elimine el texto coincidente no deseado de la cadena y obtenga el valor requerido usando js

Tengo una cadena que contiene etiquetas html y texto no deseados, por lo que quiero eliminar el texto coincidente no deseado y obtener los valores requeridos:

Código:

 var mystring = "<!-- html-text: 143 --> value 1 <!-- /html-text --><!-- html-text: 144 --> | <!-- /html-text --><!-- react-text: 145 --> value 3 <!-- /html-text --><!-- html-text: 146 -->, <!-- /html-text --><!-- html-text: 147 --> value 2 <!-- /html-text --><!-- html-text: 148 --> <!-- /html-text --><!-- html-text: 149 -->value 4 <!-- /html-text -->"; mystring = mystring.replace('<!-- html-text: 143 -->',''); console.log('str'+mystring);

Salida requerida:

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

0

Puedes hacer esto con expresiones regulares:

 var mystring = "<!-- html-text: 143 --> value 1 <!-- /html-text --><!-- html-text: 144 --> | <!-- /html-text --><!-- react-text: 145 --> value 3 <!-- /html-text --><!-- html-text: 146 -->, <!-- /html-text --><!-- html-text: 147 --> value 2 <!-- /html-text --><!-- html-text: 148 --> <!-- /html-text --><!-- html-text: 149 -->value 4 <!-- /html-text -->"; mystring = mystring.replace(/<!--.*?-->/g,''); console.log(mystring);

Explicación de expresiones regulares:

  1. <!--...--> Marca la apertura y el final de un comentario HTML
  2. .* Coincide con cualquier carácter cero o más veces
  3. ? Eliminar la "coincidencia codiciosa" (coincidir con la menor cantidad posible en lugar de la mayoría)
  4. g Global, lo que significa reemplazar todas las ocurrencias en lugar de solo una
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!