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

175
Views
Comprobar el final de la cadena (expresión regular)

Estoy tratando de comprobar si una cadena (primer argumento, str) termina con la cadena de destino dada (segundo argumento, destino). function confirmEnding(str, target) { return /target$/.test(str) } Test string confirmEnding("Bastian","n") muestra falso. que estoy haciendo mal? ¿Puedo usar una expresión regular como argumento de función? ;

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

0

Si siempre es una cadena, puede usar String#endsWith

 function checkEnd(str, target) { return str.endsWith(target); } console.log(checkEnd("Bastion", "n"));

Si desea convertir target en una expresión regular, tenga en cuenta que esto no escapará ninguno de los caracteres especiales en target . es decir checkEnd('special)',')') fallará debido a que la expresión regular no es válida:

 function checkEnd(str, target) { return new RegExp(`${target}$`).test(str); } console.log(checkEnd("Bastion", "n"));

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!