Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过JavaScript提取字符串的特定部分_Javascript_Api_Tumblr - Fatal编程技术网

通过JavaScript提取字符串的特定部分

通过JavaScript提取字符串的特定部分,javascript,api,tumblr,Javascript,Api,Tumblr,我目前正在处理一个项目的TumblrAPI 我的问题似乎是我必须能够从src url中提取用户名 http://(you).tumblr.com/api/read/json 我考虑过使用类似substr()的东西,但我不能保证要提取的字符数 有什么想法吗 使用正则表达式: > var s = 'http://you.tumblr.com/api/read/json'; > var re = /^http:\/\/(\w+)\./; > s.match(re); [ 'http

我目前正在处理一个项目的TumblrAPI

我的问题似乎是我必须能够从src url中提取用户名

http://(you).tumblr.com/api/read/json
我考虑过使用类似
substr()
的东西,但我不能保证要提取的字符数


有什么想法吗

使用正则表达式:

> 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

这里有一个不使用正则表达式的快捷方法

var str = "http://mydomain.tumblr.com/api/read/json";
var domainpart = str.substr(7, str.indexOf(".") - 7);
document.write(domainpart);

您可以使用常规表达式或
split('.')
substr()