Reputation:
I have this string
Sun-Sep-20-2015-19:11:53-GMT+0300
I want to find the delete all the string after the 19:11.. so the string will be only
Sun-Sep-20-2015
I have to search in regex the first 4 number and remove from them.. I know that I can search for 2015 but it can be also 2016..
Upvotes: 2
Views: 41
Reputation: 829
You can use a capture group to get what you want
check out this pattern (\w.+\d):
See demo here https://regex101.com/r/uJ0vD4/5
Upvotes: 0
Reputation: 700730
Instead of removing things from the string, you can pick out the part that you want:
var time = 'Sun-Sep-20-2015-19:11:53-GMT+0300';
var date = /^(.+?-.+?-\d+-\d+)/.exec(time)[0];
// show result in snippet
document.write(date);
Upvotes: 2
Reputation: 786021
You can use a capturing group:
var str = 'Sun-Sep-20-2015-19:11:53-GMT+0300';
var result = str.replace(/^(.+?\d{4}).*$/m, '$1');
Upvotes: 1