Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / What is a good regular expression to match a URL? [duplicate]

What is a good regular expression to match a URL? [duplicate]

August 1, 2021 by James Palmer

Regex if you want to ensure URL starts with HTTP/HTTPS:
https?://(www.)?[-a-zA-Z0-9@:%._+~#=]{1,256}.[a-zA-Z0-9()]{1,6}b([-a-zA-Z0-9()@:%_+.~#?&//=]*)

If you do not require HTTP protocol:
[-a-zA-Z0-9@:%._+~#=]{1,256}.[a-zA-Z0-9()]{1,6}b([-a-zA-Z0-9()@:%_+.~#?&//=]*)

To try this out see http://regexr.com?37i6s, or for a version which is less restrictive http://regexr.com/3e6m0.
Example JavaScript implementation:

var expression = /[-a-zA-Z0-9@:%._+~#=]{1,256}.[a-zA-Z0-9()]{1,6}b([-a-zA-Z0-9()@:%_+.~#?&//=]*)?/gi;

var regex = new RegExp(expression);

var t = ‘www.google.com’;

if (t.match(regex)) {

alert(“Successful match”);

} else {

alert(“No match”);

}

(https?://(?:www.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9].[^s]{2,}|www.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9].[^s]{2,}|https?://(?:www.|(?!www))[a-zA-Z0-9]+.[^s]{2,}|www.[a-zA-Z0-9]+.[^s]{2,})

Will match the following cases

http://www.foufos.gr
https://www.foufos.gr
http://foufos.gr
http://www.foufos.gr/kino
http://werer.gr
www.foufos.gr
www.mp3.com
www.t.co
http://t.co
http://www.t.co
https://www.t.co
www.aa.com
http://aa.com
http://www.aa.com
https://www.aa.com

Will NOT match the following

www.foufos
www.foufos-.gr
www.-foufos.gr
foufos.gr
http://www.foufos
http://foufos
www.mp3#.com

var expression = /(https?://(?:www.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9].[^s]{2,}|www.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9].[^s]{2,}|https?://(?:www.|(?!www))[a-zA-Z0-9]+.[^s]{2,}|www.[a-zA-Z0-9]+.[^s]{2,})/gi;

var regex = new RegExp(expression);

var check = [

‘http://www.foufos.gr’,

‘https://www.foufos.gr’,

‘http://foufos.gr’,

‘http://www.foufos.gr/kino’,

‘http://werer.gr’,

‘www.foufos.gr’,

‘www.mp3.com’,

‘www.t.co’,

‘http://t.co’,

‘http://www.t.co’,

‘https://www.t.co’,

‘www.aa.com’,

‘http://aa.com’,

‘http://www.aa.com’,

‘https://www.aa.com’,

‘www.foufos’,

‘www.foufos-.gr’,

‘www.-foufos.gr’,

‘foufos.gr’,

‘http://www.foufos’,

‘http://foufos’,

‘www.mp3#.com’

];

check.forEach(function(entry) {

if (entry.match(regex)) {

$(“#output”).append( “

Success: ” + entry + “

” );

} else {

$(“#output”).append( “

Fail: ” + entry + “

” );

}

});

Check it in rubular – NEW version
Check it in rubular – old version

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in