Created
November 28, 2011 06:08
-
-
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 圧縮も考慮します。
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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's about emoji, other special chars?