Skip to content

Instantly share code, notes, and snippets.

@codeeshop-oc
Last active February 3, 2022 12:01
Show Gist options
  • Save codeeshop-oc/2713a0abe99d6f16f01fe831934d8a19 to your computer and use it in GitHub Desktop.
Save codeeshop-oc/2713a0abe99d6f16f01fe831934d8a19 to your computer and use it in GitHub Desktop.
Ellipsis Text and add `anchor tag` to URL
/* 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