2017-10-09 1 views
2

Ich versuche, Daten zu Variable zu analysieren. Die Daten werden als Zeichenfolge im Format wie folgt dargestellt:Parsing Daten mit regulärem Ausdruck in Javascript

code   time 
0.00000  3.33333 
1.11111  4.44444 
2.22222  5.55555 

Ich bin mit Match-Methode retrive alle Wörter und Zahlen in zu Array:

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

Wörter wie Code und Zeit sind Spiel gut, aber ich habe ein Problem mit Zahlen, die auf 2 Zahlen aufgeteilt sind.

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

Was möchte ich erreichen möchte, ist dies:

code 
time 
0.00000  
3.33333 
1.11111  
4.44444 
2.22222  
5.55555 
+0

'\ W 'nicht enthalten' .'. Es sieht so aus, als ob Sie eigentlich nur Nicht-Whitespace wollen, '\ S'. – jonrsharpe

Antwort

3

für auf allen whitespaces diese und Split Verwendung von Split Let.

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>

mit Spiel Reversed funktioniert auch

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

Es funktioniert. Vielen Dank. – DuFuS

Verwandte Themen