When reading time from the back-end database, often take the whole date and month to include the time and minutes are taken, such as 2015-1-28 14:56:00, but the general we only need the front of the month and the day on the line. A simple method, directly using Split ("") [0], can be intercepted by space, The first part of the intercept is the day we want. Now it's time to say how to do it with regular expressions.
Idea: Get the space in the string, and then replace the space and all the characters after the space with NULL.
The regular \s to get the spaces
Practice:
Copy Code code as follows:
var date = "2015-12-26 15:22:00";
Console.log (Date.replace (/\s*/g, ")");
But the result is 2015-12-2615:22:00, only to remove the space, but did not remove the characters behind the space, and then to change our regular.
Copy Code code as follows:
var date = "2015-12-26 15:22:00";
Console.log (Date.replace (/\s[\x00-\xff]*/g, ")");
The result is 2015-12-26, and it meets the requirements.
This is because [\x00-\xff] matches double-byte characters, the letters and Chinese characters are matched, and the separate write \s only matches the spaces.
This article is mainly to make everyone more familiar with the regular, I hope you can enjoy.