I'm currently messing with the tumblr API for a project.
The problem I seem to have is I have to be able to extract the username from the src url eg
http://(you).tumblr.com/api/read/json
I looked into using something like substr()
but I can't guarantee the number of characters to extract.
Any ideas?
using the regular expression:
> var s = 'http://you.tumblr.com/api/read/json';
> var re = /^http:\/\/(\w+)\./;
> s.match(re);
[ 'http://you.',
'you',
index: 0,
input: 'http://you.tumblr.com/api/read/json' ]
> s.match(re)[1]
'you'
in short:
'http://you.tumblr.com/api/read/json'.match(/^http:\/\/(\w+)\./)[1]
will evaluate to
'you'
to elaborate:
^ match start of string
http:\/\/ match http://
(\w+) match group of word characters which appears 1 or more times
\. match a dot
Here's a quick and dirty way without using a regex.
var str = "http://mydomain.tumblr.com/api/read/json";
var domainpart = str.substr(7, str.indexOf(".") - 7);
document.write(domainpart);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.