Skip to content

Instantly share code, notes, and snippets.

@tomykaira
Created November 28, 2011 06:08
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 tomykaira/1399315 to your computer and use it in GitHub Desktop.
Save tomykaira/1399315 to your computer and use it in GitHub Desktop.
Character count for twitter. URLs are taken into consideration. Twitter 向けの簡易語数カウンタ。t.co による URL 圧縮も考慮します。
function count_rest (text) {
var limit = 140;
// take t.co shorten into account
// https://dev.twitter.com/blog/next-steps-with-the-tco-link-wrapper
var regex = /(?:https?\:\/\/|www\.)[^\s]+/g;
var noURL = text.replace(regex, "");
var URLs = text.match(regex);
var length = noURL.length;
if (URLs) {
URLs.forEach(function (url) {
if (url.match("https://")){
length += 21;
} else {
length += 20;
}
});
}
return limit - length;
}
@MentalGear
Copy link

What's about emoji, other special chars?

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