user5303752
user5303752

Reputation:

regex Javascript substring

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

Answers (3)

james jelo4kul
james jelo4kul

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

Guffa
Guffa

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

anubhava
anubhava

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');

RegEx Demo

Upvotes: 1

Related Questions