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 7812901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:39:50+00:00 2026-06-02T04:39:50+00:00

I’m trying to exclude some internal IP addresses and some internal IP address formats

  • 0

I’m trying to exclude some internal IP addresses and some internal IP address formats from viewing certain logos and links in the site.I have multiple range of IP addresses(sample given below). Is it possible to write a regex that could match all the IP addresses in the list below using javascript?

10.X.X.X
12.122.X.X
12.211.X.X
64.X.X.X
64.23.X.X
74.23.211.92
and 10 more
  • 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-06-02T04:39:53+00:00Added an answer on June 2, 2026 at 4:39 am

    Quote the periods, replace the X’s with \d+, and join them all together with pipes:

    const allowedIPpatterns = [ 
      "10.X.X.X", 
      "12.122.X.X",
      "12.211.X.X",
      "64.X.X.X",
      "64.23.X.X",
      "74.23.211.92"   //, etc.
    ];
    
    const allowedRegexStr = '^(?:' + 
      allowedIPpatterns.
        join('|').
        replace(/\./g, '\\.').
        replace(/X/g, '\\d+') + 
      ')$';
    
     const allowedRegexp = new RegExp(allowedRegexStr);
    

    Then you’re all set:

     '10.1.2.3'.match(allowedRegexp) // => ['10.1.2.3']
     '100.1.2.3'.match(allowedRegexp) // => null
    

    How it works:

    First, we have to turn the individual IP patterns into regular expressions matching their intent. One regular expression for "all IPs of the form ‘12.122.X.X’" is this:

    ^12\.122\.\d+\.\d+$

    • ^ means the match has to start at the beginning of the string; otherwise, 112.122.X.X IPs would also match.
    • 12 etc: digits match themselves
    • \.: a period in a regex matches any character at all; we want literal periods, so we put a backslash in front.
    • \d: shorthand for [0-9]; matches any digit.
    • +: means "1 or more" – 1 or more digits, in this case.
    • $: similarly to ^, this means the match has to end at the end of the string.

    So, we turn the IP patterns into regexes like that. For an individual pattern you could use code like this:

    const regexStr = `^` + ipXpattern. 
      replace(/\./g, '\\.').
      replace(/X/g, '\\d+') + 
      `$`;
    

    Which just replaces all .s with \. and Xs with \d+ and sticks the ^ and $ on the ends.

    (Note the doubled backslashes; both string parsing and regex parsing use backslashes, so wherever we want a literal one to make it past the string parser to the regular expression parser, we have to double it.)

    In a regular expression, the alternation this|that matches anything that matches either this or that. So we can check for a match against all the IP’s at once if we to turn the list into a single regex of the form re1|re2|re3|...|relast.

    Then we can do some refactoring to make the regex matcher’s job easier; in this case, since all the regexes are going to have ^...$, we can move those constraints out of the individual regexes and put them on the whole thing: ^(10\.\d+\.\d+\.\d+|12\.122\.\d+\.\d+|...)$. The parentheses keep the ^ from being only part of the first pattern and $ from being only part of the last. But since plain parentheses capture as well as group, and we don’t need to capture anything, I replaced them with the non-grouping version (?:..).

    And in this case we can do the global search-and-replace once on the giant string instead of individually on each pattern. So the result is the code above:

    const allowedRegexStr = '^(?:' + 
      allowedIPpatterns.
        join('|').
        replace(/\./g, '\\.').
        replace(/X/g, '\\d+') + 
      ')$';
    

    That’s still just a string; we have to turn it into an actual RegExp object to do the matching:

    const allowedRegexp = new RegExp(allowedRegexStr);
    

    As written, this doesn’t filter out illegal IPs – for instance, 10.1234.5678.9012 would match the first pattern. If you want to limit the individual byte values to the decimal range 0-255, you can use a more complicated regex than \d+, like this:

    (?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])
    

    That matches "any one or two digits, or ‘1’ followed by any two digits, or ‘2’ followed by any of ‘0’ through ‘4’ followed by any digit, or ’25’ followed by any of ‘0’ through ‘5’". Replacing the \d with that turns the full string-munging expression into this:

    const allowedRegexStr = '^(?:' + 
      allowedIPpatterns.
        join('|').
        replace(/\./g, '\\.').
        replace(/X/g, '(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])') + 
      ')$';
    

    And makes the actual regex look much more unwieldy:

    ^(?:10\.(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])\.(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5]).(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])|12\.122\....
    

    but you don’t have to look at it, just match against it. 🙂

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is

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.