0

I have problem to write a regex to remove a special and duplicated character from a string in javascript. Because I need to compare the date like type of this

2015-11-05-14:30

so I'm going to transform the date string into integer number

201511051430

and make it comparable. And so, I need to remove the character "-" & ":" !

1
  • 2
    str.replace(/[-:]/g,''); Fiddle Commented Nov 5, 2015 at 6:35

2 Answers 2

1

Simply use the following regex

/[-:]/g

OR you can simply use

/\D/g
  • \D will match any character i.e. not a digit
  • g for global modifier

Javascript :

str.replace(/[-:]/g,'');

Fiddle

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

Comments

1

You should probably be converting this into a timestamp instead of a custom "date-ish" number:

> +new Date('2015/11/05 14:30')
1446708600000

You can not only compare this, but your comparisons can be meaningful (e.g. you can figure out how many days apart 2 dates are).

Based on your original format:

var timestamp = +new Date(dateStr.replace(/-(?=\d+:)/, ' ').replace(/-/g, '/'));

2 Comments

this format is neither working in safari or in firefox
@Sean -- My bad. Forgot that they don't like dashes. Fixed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.