'Check if there is white space between letters in a string - IGNORE SPACE, TAB AND ENTER AT THE END OF THE WORD [duplicate]

I have searched and found ways to check for ALL whitespace existence between letters in a string but what about if the string contains a space, tab or enter at the end?

For example:

let str="John ";
console.log(str.includes(' '));

The above console.log will return true because there is a space at the end, however that is not relevant for me. I only wanted to check for:

let str="J o h n";

How to achieve this with Javascript ( and totally ignore space check at the end)?



Solution 1:[1]

If I understand your question correctly, this is what you want to achieve:

let str="John ";
console.log(/\s$/.test(str));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Virhile