0

After splitting a set of glossary terms with:

lines = text.split(/[\r\n]+/);

I then iterate through the array and parse out each term to properly format them during output. However, a simple check for empty strings has become much more of a headache than I could've ever imagined. Console logging gives me this:

...
"Pushing" "dyspnea: Labored or difficult respiration." //correct
"Pushing" ""
...

Things I have tried in order to find these empty strings:

line === ""
line.length == 0
if(line)
isNaN(line.charCodeAt(0))
typeof line == "undefined"

And various combinations of the list above. On recommendation from a coworker, I checked the line endings of the input text, but it all seemed normal.

I'm sure I'm just doing something really stupid, but the solution has eluded me for far too long. Any help/suggestions would be greatly appreciated!

Edit: Thanks for the suggestions everyone. Alas, the problem persists...

Also, I forgot to mention, but I have tried both trimming and replacing whitespace in each line after the split, but came up with nothing.

As requested, here is more relevant code.

var text = "";
var end = /\x2E\s\x5B/gm; // ". ["
var lines = [];
var terms = [];

text = document.getElementById("terms").value;
lines = text.split(/[\r\n]+/);
parseText();

function parseText() {
    var i = 0;
    while(i < lines.length) {
        var line = lines[i];
        endIndex = lines[i].search(end);

        if(line != "" || line != " " || line.length != 0 ) {
            parseTerm(lines[i].substring(0, endIndex+1));
        }

        i++;
}
2
  • 2
    Can we see a complete code example which reproduces the issue? Your empty checks should work. Commented Aug 12, 2014 at 15:33
  • @Vache More relevant code has been added. Let me know if you see anything. Commented Aug 12, 2014 at 16:30

3 Answers 3

2

As the previous answer stated issue is probably whitespace, you can use the trim function to shorten your code:

if (line.trim() == "") {
    alert("Blank");
}
Sign up to request clarification or add additional context in comments.

2 Comments

No dice. Same output.
it's not resolve problem, then spaces inside text "Hello World!"
0

maybe string is not a "", but " "?

so check not only zero length, but "white space"

if(st1 == "" || st1 == " " || st1.length == 0 ){
    console.log("find empty")    
}

2 Comments

Alternatively: st1.trim() === ""
A good thought, I hadn't looked into this much, but still nothing.
0

Turns out that in my input there was a line with two spaces. I have NO idea why this was causing problems, considering the split was specifically on the pattern described above, but replacing instances of too much whitespace fixed the issue. The new line:

text.replace(/\s\s+/g, " ").split(/[\r\n]+/);

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.