1

I'm having problems using split to try and parse a text file. the text file is as shows:

123.0   321.02
342.1   234.03
425.3   326.33
etc.    etc.

When I read using a FileReader() and doing a readAsText call on the file, the file appears in a string as such:

"123.0 321.02\r\n342.1 234.03\r\n ..." (How it appears in Firebug)

Currently I'm trying to split it like this:

var reader = FileReader();
reader.readAsText(f);
alert(reader.result);
var readInStrings = reader.result.split(/|\s|\n|\r|/);

but when I do this, the resulting array has values as shown:

["123.0", "321.02", "", "342.1", "234.03", "" etc....]

Can anyone explain to me where the values of {""} in the array are coming from and how to correctly split such a file as to only get the number strings as the values?

Any help would be greatly appreciated, thanks!

Note*: Currently doing this in javascript

1
  • Those are probably the carriage return and line-feed characters... Commented Jun 19, 2013 at 17:43

1 Answer 1

1

This is likely due to splitting on each newline and carriage return character rather than each bundle of such characters. To prevent this issue, you could cluster them in the regular expression such as /\s+/ or something similar.

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

4 Comments

Thanks a lot! I found some pages on regex but most were rather confusing! This worked perfectly!
This is a good answer. One warning. If you have a whitespace character or newline at the end, you'll get an empty result at the end of the array. So trim the string before the split.
One addendum: in JavaScript, \s is the same as [ \t\r\n], so you'd really only need /\s+/.
You are correct; a sloppy oversight on my part. I've updated my answer. Thanks.

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.