Skip to content

Instantly share code, notes, and snippets.

@oraricha
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oraricha/9e10d34c2f0104604496 to your computer and use it in GitHub Desktop.
Save oraricha/9e10d34c2f0104604496 to your computer and use it in GitHub Desktop.
url regex for ftp/http/https, can be without protocol name as well - using with contenteditable
var urlRegex = /^((?:ftp|http|https)?:\/\/)?((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3}))(\:\d+)?(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_.~+=-]*)?(\#[-a-z\d_]*)?$/i;
/* the following will enforce appearance of 'www' while the protocol is optional, but if appears, must be correct */
urlRegex = /^((ftp|http|https)?:(\/\/)|(w{3}\.))((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3}))(\:\d+)?(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_.~+=-]*)?(\#[-a-z\d_]*)?$/i;
/*check url on contenteditable element*/
var currentText,
$element = $("[contenteditable='true']");
var replaceAll = function (find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
};
// first replace all &npsp; chars with a regular space char
currentText = replaceAll(String.fromCharCode(160)," ", currentText);
// then trim all spaces
currentText = $this.text().trim();
// do whatever
@oraricha
Copy link
Author

@oraricha
Copy link
Author

The first regex above will accept: a.com | www.com | http://www.a.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment