Last active
February 3, 2022 12:01
-
-
Save codeeshop-oc/2713a0abe99d6f16f01fe831934d8a19 to your computer and use it in GitHub Desktop.
Ellipsis Text and add `anchor tag` to URL
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
/* Ellipsis Text and add `anchor tag` to URL */ | |
String.prototype.convertText = function (ellipse = 0, suffix = '...') { | |
if (!this.length) return; | |
let urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g; | |
this.replace(urlRegex, function (url) { | |
let hyperlink = url; | |
if (!hyperlink.match('^https?:\/\/')) { | |
hyperlink = 'http://' + hyperlink; | |
} | |
return '<div><a href="' + hyperlink + '" target="_blank" rel="noreferrer">' + url + '</a></div>' | |
}); | |
return ellipse && this.length > ellipse ? this.substring(0, ellipse).trim() + suffix : this.substring(0, this.length) | |
} | |
console.log('Just How you Do It'.convertText(5)) | |
console.log('Cool'.convertText(5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment