Javascript相当于爆炸

时间:2013-03-12 04:41:48

标签: javascript

我最初的想法是从使用JSON接收的网址中抓取一个特定的数字

http://fh13.fhcdn.com/static/img/nations/14.png

我如何使用Javascript只收到 14 ?我会使用split()还是不一样?

7 个答案:

答案 0 :(得分:1)

抓住所有数字:

var url = "http://fh13.fhcdn.com/static/img/nations/14.png";
url.match(/\d+/g);   // returns ["13","14"], so url.match(/\d+/g).pop() == "14"

或者抓住“14.png”,然后是parseInt():

parseInt( url.match( /\d+\D*$/g ) );

答案 1 :(得分:1)

你可以这样做:

var l = "http://fh13.fhcdn.com/static/img/nations/14.png";
var larr = l.split("/");
var laast = larr.pop();
console.log(laast.split(".")[0]); //gets 14

答案 2 :(得分:1)

是分开是要走的路......

var n = '14.jpg';
var p = n.split('.');
console.log(p[0]);

此外,如果文件名中有一个点,您可以尝试:

var n = 'file.14.jpg';
var p = n.split('.');
p.pop(); //removes the extension
console.log(p.join('.'));

答案 3 :(得分:1)

var url = "http://fh13.fhcdn.com/static/img/nations/14.png";

var index = url.lastIndexOf("/");
var dotIndex = url.indexOf(".", index);

console.log(url.substring(index+1,dotIndex));  // Prints 14

答案 4 :(得分:1)

如果你总是有相同的结构可以使用

var url = "http://fh13.fhcdn.com/static/img/nations/14.png";
var n = ((url.split(".")[2]).split("/"))[4]; //return 14

答案 5 :(得分:0)

您可以使用split()。它的第一个参数可以是字符串或正则表达式。因此,如果您想基于多个分隔符拆分字符串,您可以只是形成字符的快速正则表达式。

答案 6 :(得分:0)

字符串的.split更等同于preg_split(或已弃用的split),而不是explode,因为它也采用正则表达式及其方法使用方法不同,但可以几乎相同的方式使用。

url.substring(url.lastIndexOf('/') + 1).split('.')[0]

您还可以使用.lastIndexOf('.')作为substring的第二个参数。

相关问题