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

510
Views
how can I limit dot only once or optional in regex

A regex is needed which should have only special character dot which should either be optional or occur only once.

pattern = /^([A-Za-z.]+)$/;
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Without more information I'd use

/^(?!$)[a-z]*\.?[a-z]*$/i

The negative lookahead prevents empty matches.

See this demo at regex101

about 4 years ago · Juan Pablo Isaza Report

0

Here are some ways to do it:

  1. Deal separately where the input has has one dot, with optional letters surrounding it, or no dot (but then having at least one letter):

    /^([a-z]*\.[a-z]*|[a-z]+)$/i

  1. Just capture letters and dots like you did, but don't allow the input to have two dots, using a negative look ahead:

    /^(?!(.*\.){2})([a-z.]+)$/i

  2. Capture optional letters then an optional point and then optional letters, but forbid an empty input with a negative look-ahead:

    /^(?!$)([a-z]*\.?[a-z]*)$/i

about 4 years ago · Juan Pablo Isaza Report

0

If in any case the dot should not be at the start or end and not match an empty string, you can start the match with 1 or more chars a-z and then optionally match a dot and again 1 or more chars a-z:

/^[a-z]+(?:\.[a-z]+)?$/i

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!