我目前正在处理一个项目的tumblr API。
我似乎遇到的问题是我必须能够从src url中提取用户名。
http://(you).tumblr.com/api/read/json我考虑过使用像substr()这样的东西,但我不能保证要提取的字符数。
有什么想法吗?
发布于 2012-01-27 00:39:17
使用正则表达式:
> 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'简而言之:
'http://you.tumblr.com/api/read/json'.match(/^http:\/\/(\w+)\./)[1]将计算为
'you'详述:
^ match start of string
http:\/\/ match http://
(\w+) match group of word characters which appears 1 or more times
\. match a dot发布于 2012-01-27 00:44:57
这里有一种不使用正则表达式的又快又脏的方法。
var str = "http://mydomain.tumblr.com/api/read/json";
var domainpart = str.substr(7, str.indexOf(".") - 7);
document.write(domainpart);https://stackoverflow.com/questions/9021501
复制相似问题