Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Detect a space in a string

I am writing a lexer for a programming language.

I have managed to detect all characters but spaces (not general whitespaces, just spaces).

I’ve tried just using letter == " " and letter === " ".
I’ve also looked at some SO question that recommend using some Regex, but that also didn’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Relevant code snippet (this is part of a bigger Lexer class):

generateOutput(input) {
        const wordList = input.split(' ');
        const keyword = wordList[0];
        const identifier = wordList[1];

        const output = [
            "[KEYWORD] " + keyword.toUpperCase(),
            "[IDENTIFIER] " + identifier
        ]

        for (let i = 2; i < wordList.length; i++) {
            const word = wordList[i];
            const words = word.split("");
            words.forEach((letter) => {
                if (upperCase.includes(letter)) {
                    output.push("[KEY] U_" + letter)
                } else if (lowerCase.includes(letter)) {
                    output.push("[KEY] L_" + letter)
                } else if (symbols.includes(letter)) {
                    output.push("[SYMBOL] " + letter)
                } else if (letter === " ") {
                    output.push("[KEY] SPACE")
                }
            })
        }

        return output
    }

>Solution :

You used input.split(' '), like I said in my comment. That deleted all of the spaces.

var a = "this is a sentence"
console.log( a.split( " " ) );
//["this","is","a","sentence"] <- contains absolutely no spaces
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading