JavaScript uses regular expressions to implement character _javascript techniques after removing spaces

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.