2

I'm trying parse data to variable. Data are represented as string in format like this :

code          time
0.00000       3.33333
1.11111       4.44444
2.22222       5.55555

I'm using match method to retrive all words and numbers in to array:

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

Words like code and time are match good but I have problem with numbers which are splitted to 2 numbers.

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

What I would like to achieve is this :

code
time
0.00000       
3.33333
1.11111       
4.44444
2.22222       
5.55555
1
  • \w doesn’t include .. It looks like you actually just want non-whitespace, \S. Commented Oct 9, 2017 at 7:12

1 Answer 1

3

Let's use split for this and split on all 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>

Reversed with match works too

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.