I am trying to use jQuery to break right ascension and declination data into their constituents (hours, minutes, and seconds) and (degrees, arc-minutes, and arc-seconds), respectively from a string and store them in variables as numbers. For example:
我试图使用jQuery分别从字符串中将正确的提升和赤字数据分解为它们的成分(小时,分钟和秒)和(度,弧分和弧秒),并将它们作为数字存储在变量中。例如:
$dec = "-35:48:00" -> $dec_d = -35, $dec_m = 48, $dec_s = 00
Actually, the data resides in a cell (with a particular class ('ra')) in a table. At present, I have gotten this far:
实际上,数据驻留在表格中的单元格(具有特定类(“ra”))中。目前,我已经走到了这一步:
var $dec = $(this).find(".ra").html();
This gives me the declination as a string but I cannot figure out how to parse that string. I figured out the regular expression (-|)+\d+
(this gives me -35 from -35:48:00) to get the first part. How do I use that in conjunction with my code above?
这给了我作为字符串的偏差,但我无法弄清楚如何解析该字符串。我想出了正则表达式( - |)+ \ d +(这给了-35从-35:48:00得到-35)来得到第一部分。我如何结合上面的代码使用它?
2 个解决方案
#1
This should do it:
这应该这样做:
var dec = '-35:48:00';
var parts = dec.split(':');
parts[0]
would then be -35
, parts[1]
would be 48
, and parts[2]
would be 00
part [0]则为-35,part [1]为48,part [2]为00
You could run them all through parseInt(parts[x], 0)
if you want integers out of the strings:
如果你想要整数字符串,你可以通过parseInt(parts [x],0)运行它们:
var dec_d = parseInt(parts[0], 10);
var dec_m = parseInt(parts[1], 10);
var dec_s = parseInt(parts[2], 10);
I should point out this really has nothing to do with jQuery and is a Javascript problem (past getting the values out of the HTML document, at least) - The practice of prefixing a variable with a $
is usually done to signify that the variable contains a jQuery collection. Since in this situation it contains HTML, it is a little misleading
我应该指出这与jQuery无关,并且是一个Javascript问题(过去从HTML文档中获取值,至少) - 通常使用$前缀变量来表示变量包含一个jQuery集合。由于在这种情况下它包含HTML,因此有点误导
#2
Use String.match()
$dec = "-35:48:00";
matches = $dec.match(/-*[0-9]+/g);
for (i=0;i<matches.length;i++){
alert(matches[i]);
}
#1
This should do it:
这应该这样做:
var dec = '-35:48:00';
var parts = dec.split(':');
parts[0]
would then be -35
, parts[1]
would be 48
, and parts[2]
would be 00
part [0]则为-35,part [1]为48,part [2]为00
You could run them all through parseInt(parts[x], 0)
if you want integers out of the strings:
如果你想要整数字符串,你可以通过parseInt(parts [x],0)运行它们:
var dec_d = parseInt(parts[0], 10);
var dec_m = parseInt(parts[1], 10);
var dec_s = parseInt(parts[2], 10);
I should point out this really has nothing to do with jQuery and is a Javascript problem (past getting the values out of the HTML document, at least) - The practice of prefixing a variable with a $
is usually done to signify that the variable contains a jQuery collection. Since in this situation it contains HTML, it is a little misleading
我应该指出这与jQuery无关,并且是一个Javascript问题(过去从HTML文档中获取值,至少) - 通常使用$前缀变量来表示变量包含一个jQuery集合。由于在这种情况下它包含HTML,因此有点误导
#2
Use String.match()
$dec = "-35:48:00";
matches = $dec.match(/-*[0-9]+/g);
for (i=0;i<matches.length;i++){
alert(matches[i]);
}