0
var currentPageURL = "www.javascript.com/dun/1.70 80";
var url = getMeasurementsFromUrl(currentPageURL)

document.write(url);

function getMeasurementsFromUrl(url)
{

var urlMeasurmentString = url.substring(url.lastIndexOf('/dun/')+5);

urlMeasurmentString =urlMeasurmentString.replace("abcdefghijklmnopqrstuvwxyz",""); 
urlMeasurmentString =urlMeasurmentString.replace(",","."); 
urlMeasurmentString =urlMeasurmentString.replace("/","."); 
urlMeasurmentString =urlMeasurmentString.replace("-",".");
urlMeasurmentString =urlMeasurmentString.replace(",","."); 
urlMeasurmentString =urlMeasurmentString.replace("/","."); 
urlMeasurmentString =urlMeasurmentString.replace("-",".");
return urlMeasurmentString;
}

我需要将输出“1.70 80”拆分为两个单独的整数。 http://jsfiddle.net/mqWGm/

4

3 回答 3

0

如果你想要 1.70 和 80 那么你可以做

urlMeasurementString.split(" ");

这将返回一个数组,其中第一个元素是 1.70,第二个元素是 80;

于 2012-11-01T21:06:17.910 回答
0
var currentPageURL = "www.javascript.com/dun/1.70 80";
var url = getMeasurementsFromUrl(currentPageURL)
var integers = url.split(' ');
for(var i = 0; i<integers.length; i++)
{
    integers[i] = parseFloat(integers[i],2);
}
console.log(integers);

function getMeasurementsFromUrl(url)
{

var urlMeasurmentString = url.substring(url.lastIndexOf('/dun/')+5);

urlMeasurmentString =urlMeasurmentString.replace("abcdefghijklmnopqrstuvwxyz","");
urlMeasurmentString =urlMeasurmentString.replace(",",".");
urlMeasurmentString =urlMeasurmentString.replace("/",".");
urlMeasurmentString =urlMeasurmentString.replace("-",".");
urlMeasurmentString =urlMeasurmentString.replace(",",".");
urlMeasurmentString =urlMeasurmentString.replace("/",".");
urlMeasurmentString =urlMeasurmentString.replace("-",".");
return urlMeasurmentString;
}

或更好:

var currentPageURL = "www.javascript.com/dun/1.70 80";
console.log(getIntegers(currentPageURL));

function getIntegers(url){
    url = url.split('/');
    url = url[url.length-1].split(' ');
    for(var i = 0; i<url.length; i++)
    {
        url[i] = parseFloat(url[i],2);
    }
    return url;
}
于 2012-11-01T21:08:27.470 回答
0

1.70 不是整数,但如果您需要从字符串“1.70 80”中获取数字,您可以执行以下操作:

var atoms = url.split(" ");
var num1 = parseFloat(atoms[0]);
var num2 = parseFloat(atoms[1]);
于 2012-11-01T21:09:17.050 回答