Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6668965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:05:51+00:00 2026-05-26T03:05:51+00:00

I am trying to write a regex for allowing 3 digit numbers followed by

  • 0

I am trying to write a regex for allowing 3 digit numbers followed by optional decimal, integer part cant be greater than 180 or smaller than -180. example of valid enteries are 156.56, 56.778, 9, 6.7.etc etc.
So far I came up with this ^(\+|-)?:180|1[0-7][0-9]|[0-9][0-9]|[0-9]?([\.][0-9]{1,3})?$ but these seems to match 195.678 too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:05:52+00:00Added an answer on May 26, 2026 at 3:05 am

    Instead of doing that, why not just have two checks? One makes sure that you match the pattern, and the other makes sure that the value is between -180 and 180.

    /-?\d{3}(\.\d{1,3})?/.test(val) && (val <= 180 && val >= -180)
    

    The resulting solution is far easier to understand and maintain, and not to mention, saner :).

    The reason your regex doesn’t work is that the ^ and $ don’t match across the entire pattern. You have the different conditions in your regex that are separated by |. The ^ is associated with the first option, and the $ is associated with the last option. This is easy to see in the following example:

    Assume you had the regex /^[0-9][0-9]|[0-9]$/. If you tested this against 12345 what would you get? You might be inclined to say that it would return false, but it actually returns true. This is because there is a valid match! The 12 in 12345 will match against ^[0-9][0-9]. So what you’re saying is that you want to match against any two numbers at the beginning of the string or against a number at the end of the string. Basically any string that has at least two numbers at the beginning or one number at the end, will match. So 12abc will match, as will abc1.

    However, if you changed the regex to /^([0-9][0-9]|[0-9])$/, only one and two-digit numbers will match. This is because the beginning and end-of-string anchors are outside the matching group, meaning the entire group has to match. This is something that you have to consider when you have conditions in your regex.

    EDIT

    Also, Tim points out:

    … there is a colon where it shouldn’t be: ^(+|-)?:180 matches :180, +:180 or -:180 and nothing else

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a regex to validate part or model numbers. These can
I am trying to write a regex to get the numbers from strings like
I am trying to write a regex that matches the following: first an optional
I'm trying to write a regex pattern that will find numbers with two leading
I am trying to write a regex that matches all numbers (0-9) and #
So, I'm trying to write a regex that matches all numbers. Here is that
I'm trying to write a regex function that will identify and replace a single
I'm trying to write a regex that will parse out the directory and filename
I'm trying to write a regex expression to match the src, width and height
I'm trying to write a regex that will match everything BUT an apostrophe that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.