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

234
Views
Js Regex solo permitió caracteres una ocurrencia

Probé la expresión regular muy duro pero no pude obtener el resultado deseado.

  1. Debe comenzar con números y un máximo de 4
  2. seguido de un solo alfabeto permitido

Por favor, eche un vistazo al siguiente código.

 var strings = [ '1h', //should match '13s', //should match '30m', //should match '42hr', //should not match '8 hours ', //should not match '765765', //should not match '5sec', //should not match '23445345345s', //should not match '335m' //should match ]; for (var i = 0; i < strings.length; i++) { var match = strings[i].match(/\d{4}[h|m|s]{1}/g); console.log(strings[i], (match ? "Match" : "Not match")); }

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

0

Su expresión regular debe ser:

 /^\d{1,4}[hms]$/gm

Primero, necesita 4 dígitos con d{4}

Cámbielo a d{1,4} para que sea de uno a cuatro dígitos

Luego agregue un $ para indicar el final de la cadena, para que no permita más caracteres después de la letra

Consulte Regex101 , realmente útil para probar y comprender expresiones regulares

about 4 years ago · Juan Pablo Isaza Report

0

He agregado un ^ para que coincida desde el principio

 strings.filter(x => x.match(/^\d{1,4}[hms](?!\w)/g) )
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!