How hard is it to check for whitespace?

  • TehPers@beehaw.org
    link
    fedilink
    English
    arrow-up
    5
    ·
    18 hours ago

    JavaScript takes yet another approach. It doesn’t have an equivalent of the Character.isWhitespace() method, but we can use the \s character class in a regular expression

    \s might be the most obvious whitespace test, but the expectations are a bit more ambiguous when it comes to regex. Every regex runtime is different, so without looking it up, you’re going to end up with behavior you might not expect.

    Instead, if you want to match Unicode whitespace, you should use \p{White_Space} in Unicode-aware mode (u or v flag).

    I haven’t looked through any other runtimes, but something tells me the inconsistencies won’t stop at these three.

    According to the docs, Rust’s char::is_whitespace checks if the value has the White_Space property, and regex does the same in Unicode mode (which is the default).