与任何正则表达式一样,有无限可能的解决方案可以让事情飞起来。在下面的 main.js 中查找parseValues(...)
函数。
jsFiddle在这里:http: //jsfiddle.net/uwe_guenther/rYSwx/
索引.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id='output'></div>
<script src='main.js'></script>
</body>
</html>
main.js
(function (document) {
function parseValues(values) {
var results = [];
var regex = /T\.(\d+)?.*/;
var len = values.length;
var i;
var result;
for (i = 0; i < len; i++) {
result = values[i].replace(regex, '$1');
if (result) {
if (results.indexOf(result) < 0) {
results.push(result);
}
}
}
return results;
};
function main() {
var values = [
'T.220431.2 = 1.16363965404949',
'T.220431.1 = 1.16363965404949',
'T.3923331.2 = 0.335752950772576',
'T.3923331.1 = 0.335752950772576',
'T.4125531.2 = 0.335752950772576',
'T.4125531.1 = 0.335752950772576'
];
var results = parseValues(values);
var output = document.getElementById('output');
var len = results.length;
var i;
for (i = 0; i < len; i += 1) {
output.innerHTML += results[i] + '<br/>';
}
};
main();
})(document);