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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:00:33+00:00 2026-06-06T20:00:33+00:00

I have a JS function which is passed a string that a RegEx is

  • 0

I have a JS function which is passed a string that a RegEx is run against, and returns any matches:

searchText= // some string which may or may not contain URLs
Rxp= new RegExp("([a-zA-Z\d]+://)?(\w+:\w+@)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(/.*)?/ig")
return searchText.match(Rxp);

The RegExp should return matches for any of the following (and similar derivations):

  • google.com
  • http://www.google.com
  • http://www.google.com
  • http://google.com
  • google.com?querystring=value
  • http://www.google.com?querystring=value
  • http://www.google.com?querystring=value
  • http://google.com?querystring=value

However, no such luck. Any suggestions?

  • 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-06T20:00:35+00:00Added an answer on June 6, 2026 at 8:00 pm

    In a string, \ has to be escaped: \\.

    First, the string is interpreted. \w turns in w, because it has no significant meaning.
    Then, the parsed string is turned in a RegEx. But \ is lost during the string parsing, so your RegEx breaks.

    Instead of using the RegExp constructor, use RegEx literals:

    Rxp = /([a-zA-Z\d]+:\/\/)?(\w+:\w+@)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(\/.*)?/ig;
    // Note: I recommend to use a different variable name. Variables starting with a
    //  capital usually indicate a constructor, by convention.
    

    If you’re not 100% sure that the input is a string, it’s better to use the exec method, which coerces the argument to a string:

    return Rxp.exec(searchText);
    

    Here’s a pattern which includes the query string and URL fragment:

    /([a-zA-Z\d]+:\/\/)?(\w+:\w+@)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(\/[^?#\s]*‌)?(\?[^#\s]*)?(#\S*)?/ig
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which checks the passed value and returns false or true,
Let's say I have a function which is being passed a string which originally
I have a string: the quic Which is passed on to a function where
I have a function which accepts a string parameter such as: var1=val1 var2=val2 var3='a
I have a function which returns a one-sided intersection of values between two input
I have a function which requires to be run in STA apartment state. I
I have a PHP function which takes a passed url and creates a clean
I have a function which restricts access to some of the places on my
I have a string passed into a function, I would like to compare the
I have a function called CreateCriteriaExpression that takes a json string and creates a

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.