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

312
Views
Regex to match name prefix

I need regex conditions that matches any string satisfying the following condition:

  1. String starts with the prefix Mr., Mrs., Ms., Dr., or Er.

I tried /(?<!prefix )Mr.[a-zA-Z]/ so far, but it didn't work.

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

0

(Mr|Mrs|Ms|Dr|Er)\.[a-zA-Z]+

Explanation:

  • (Mr|Mrs|Ms|Dr|Er) - uses grouping and alternation to match any of the following: Mr, Mrs, Ms, Dr, Er

  • \. - matches the literal . character

  • [a-zA-Z]+ - matches one or more occurence of an uppercase/lowercase letter.

const regex = /(Mr|Mrs|Ms|Dr|Er)\.[a-zA-Z]+/;

function validate(str){
  console.log(`${str} matches regex? ${regex.test(str)}`)
}

validate("Mr.John")
validate("Mrs.John")
validate("Ms.John")
validate("Dr.John")
validate("Er.John")
validate("Spectric.Spectric")

about 4 years ago · Juan Pablo Isaza Report

0

To match all variants you can write the pattern as:

^(?:Mrs?|Ms|[DE]r)\.

Regex demo

If there has to be a char A-Za-z following, you could also match optional whitespace chars preceding the character class

^(?:Mrs?|Ms|[DE]r)\.\s*[A-Za-z]

Regex demo

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!