Javascript uses regular expressions to remove characters After Spaces

Source: Internet
Author: User

Javascript uses regular expressions to remove characters After Spaces

This article describes how to use a regular expression in javascript to remove characters After spaces. For more information, see

 

When reading data from the backend database, we often get the entire date, month, day, including hour, minute, and second, for example, 14:56:00, but generally we only need the previous year, month, and day. A simple method is to directly use split ("") [0] to intercept a space and obtain the first part of the screenshot, that is, the year, month, and day we want. how to Implement the regular expression.

Train of Thought: Get the space in the string, and then replace all the characters after the space and space with null.

Returns the space regular \ s.

Practice:

 

The Code is as follows:


Var date = "15:22:00 ";
Console. log (date. replace (/\ s */g ,''));

 

However, the result is 2015-12-2615: 22: 00. Only the space is removed, but the characters after the space are not removed. Then, modify the regular expression.

 

The Code is as follows:


Var date = "15:22:00 ";
Console. log (date. replace (/\ s [\ x00-\ xff] */g ,''));

 

The result is, which meets the requirements.

This is because [\ x00-\ xff] will match double-byte characters, letters and Chinese characters will be matched, and separate write \ s only matches space.

This article aims to make everyone more familiar with regular expressions and hope everyone will like it.

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.