This article mainly introduces how to use a regular expression in javascript to remove characters After spaces. If you need a friend, refer to the time when reading data from the backend database, we often get the entire date, year, 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 will only match spaces.
This article aims to make everyone more familiar with regular expressions and hope everyone will like it.