Compare content from a textfile in javascript

52 views Asked by At

I'm just trying to split textfile data into words and then comparing it but it don't return true.

var contents = e.target.result.split("\n");
if (contents[0] === "abc"])
    alert("abc=abc");

above is the simplest code i am trying to test but even on a single word it gives false. Help

2

There are 2 answers

0
Naveen Kumar Alone On

Why you are placed ] in your if condition after "abc"? Remove it

Reference

 if (contents[0] === "abc"])
                          ^
0
tavnab On

I'm just trying to split textfile data into words

Based on this, I'm assuming you actually want to split your text by whitespace, rather than just unix newlines (\n). If that's the case, try passing a RegExp for one or more consecutive whitespace characters (/\s+/), instead of \n as your separator:

var contents = e.target.result.split(/\s+/);
if (contents[0] === "abc")
    alert("abc=abc");

This will tokenize e.target.result using consecutive whitespace as separators.