Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript replace characters

I want to replace all occurent of "-", ":" characters and spaces from a string that appears in this format:

"YYYY-MM-DD HH:MM:SS"

something like:

var date = this.value.replace(/:-/g, "");
like image 853
mustapha george Avatar asked Aug 17 '11 19:08

mustapha george


2 Answers

You were close: "YYYY-MM-DD HH:MM:SS".replace(/:|-/g, "")

like image 190
Gabi Purcaru Avatar answered Oct 22 '22 22:10

Gabi Purcaru


/:-/g means ":" followed by "-". If you put the characters in [] it means ":" or "-".

var date = this.value.replace(/[:-]/g, "");

If you want to remove spaces, add \s to the regex.

var date = this.value.replace(/[\s:-]/g, "");
like image 20
Rocket Hazmat Avatar answered Oct 22 '22 20:10

Rocket Hazmat