Search code examples
javascriptjquerywords

Get last word js


This removes the last word. How do I get the last word (Stripe)?

var BaseShirtURL = "Base Shirt Draped with Fabric Stripe";
BaseShirtURL = BaseShirtURL.substring(0, BaseShirtURL.lastIndexOf(" "));
alert(BaseShirtURL);

Solution

  • To get the last word:

    BaseShirtURL.split(' ').pop();
    

    And to remove the last word you can do this:

    BaseShirtURL = BaseShirtURL.replace(/\w+$/,'');