2

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..

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.