Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip the ending of a specific string off end of a pattern [closed]

The string I have is "Last Updated on November 7, 8:00 PM AST". Using regex I want to remove "AST" off of the end of it. How do I go about doing so?

like image 870
user2961971 Avatar asked Dec 15 '22 04:12

user2961971


2 Answers

Using regex, you could do this:

var string = "Last Updated on November 7, 8:00 PM AST";
var modified = string.replace(/ \w{3}$/, '');

Though, if it is always going to be the last three characters (and the space) in the string, this will probably be faster:

var modified = string.substring(0, string.length - 4);
like image 59
kalley Avatar answered Dec 17 '22 17:12

kalley


No need regex, just get the substring up till the last index of a space character.

var s = "Last Updated on November 7, 8:00 PM AST";
s.substr(0, s.lastIndexOf(" ")) // "Last Updated on November 7, 8:00 PM"
like image 27
thgaskell Avatar answered Dec 17 '22 18:12

thgaskell