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

186
Views
Check the end of string (regular expression)

i am trying to heck if a string (first argument, str) ends with the given target string (second argument, target). function confirmEnding(str, target) { return /target$/.test(str) } Test stringconfirmEnding("Bastian","n") show false. What i am doing wrong? Can i use regular expression as argument of function? ;

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

0

If it's always a string, then you can just use String#endsWith

function checkEnd(str, target) {
  return str.endsWith(target);
}

console.log(checkEnd("Bastion", "n"));

If you want to cast target into a regex, but note that this won't escape out any of the special characters in target. i.e. checkEnd('special)',')') will fail due to the regex being invalid:

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!