How to check if last character in a string is 'space'?

You can check whether the text value ends with space by the following regular-expression:

/\s$/

/\s$/ means one space at the end of the string.

JSFiddle

JavaScript

var mystring = $("#someid").text();

$("#someid").click( function (event) {
    if(/\s+$/.test(mystring)) {
        $("#result").text("space");    
    } else {
        $("#result").text("no space");

    }    
}); 

As jfriend00 noticed \s does not means only space, it's white-space [i.e. includes tab too (\t)]

If you need only space use: / $/.


Do this way:-

/(.*)\s+$/

JS:

var mystring = $("#someid").text();

$("#someid").click(function(event) {
    if(/(.*)\s+$/.test(mystring)) {
        $("#result").text("space");
    }
    else
    {
        $("#result").text("no space");    
    }
}); 

Refer LIVE DEMO


A more simple and clear solution would be using .endsWith()

"hallo ".endsWith(" "); // true