• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

306
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 3 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 3 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 3 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 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error