2017-10-09 37 views
2

我試圖將數據解析爲變量。數據表示爲字符串格式是這樣的:在javascript中使用正則表達式解析數據

code   time 
0.00000  3.33333 
1.11111  4.44444 
2.22222  5.55555 

我用比賽的方法以檢索所有的文字和數字的數組:

result = mystring.match(/(\w+)/g); 

詞如代碼和時間的比賽不錯,但我數字分成2個數字有問題。

code 
time 
0 
00000  
3 
33333 
1 
11111  
4 
44444 
2 
22222  
5 
55555 

我想實現的是:

code 
time 
0.00000  
3.33333 
1.11111  
4.44444 
2.22222  
5.55555 
+0

'\ w'不包括'.'。看起來你實際上只是想要非空白字符'\ S'。 – jonrsharpe

回答

3

再拿分,這和分裂的所有whitespaces

var match = document.querySelector("pre").textContent.split(/\s+/g); 
 

 
console.log(match);
<pre> 
 
code   time 
 
0.00000  3.33333 
 
1.11111  4.44444 
 
2.22222  5.55555 
 
</pre>

與之相匹配的反向工程太

var match = document.querySelector("pre").textContent.match(/\S+/g); 
 

 
console.log(match);
<pre> 
 
code   time 
 
0.00000  3.33333 
 
1.11111  4.44444 
 
2.22222  5.55555 
 
</pre>

+1

它的工作原理。謝謝。 – DuFuS