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.