Last active
August 29, 2015 14:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.regex101.com/