Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete spaces before and after using regular expressions in javascript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use regular expressions to delete spaces before and after javascript, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Remove the first space

Str=str.replace (/ ^\ s + |\ s for example)

Js regular expression removes spaces before and after a string

String.prototype.trim=function () {var reSpace=/ ^\ s * (. *?)\ s return this.replace (reSpace, "$1");}

Let's analyze the regular expression of the second line

^ Line start

\ s*

Match all spaces in front of characters, greedy pattern repetition

(. *)

Capture group, reluctantly pattern repeated matching of any character, that is, we eventually need (after the removal of spaces), this is not very easy to understand (I think)

First: I originally thought that in the capture group, we should also judge that the first character should not be written in the form of ([^\ s +]), but this is completely unnecessary because\ s* in front of the capture group can already capture all the space characters at the beginning. You think the starting range of the characters of the capture group is not the same as that of the regular expression. Khan is a little unclear.

Second: among them? His function is to force the pattern to repeat the preceding characters. What does that mean? That is, if I use (. * a) to match the aaaaaaa string, the result is the (aaaaaaa) source string, which is called the greedy pattern. If I use (. *? a) to match the aaaaaaa, it will match the first a, then the second a, then the third a. . This is called grudging pattern matching and in some places it is also called lazy pattern matching. The popular point (everyone likes the popular explanation, hehe) is that the former matches as many characters as possible from back to front, while the latter matches from back to back.

Third: should we care about the following spaces in the capture group? Because the "." in the capture group It is also possible to match spaces, which I wasted most of my time thinking about before. In fact, this is the same reason as considering whether to exclude the space in front of the capture group, the latter\ s* has been dealt with for us.

\ s* match the space after the character

-dividing line-

Customize three trim () functions to filter the spaces on the left and right sides of the string.

/ / js remove spaces function / / here add three members to the string class: String.prototype.Trim = function () {return Trim (this);} String.prototype.LTrim = function () {return LTrim (this);} String.prototype.RTrim = function () {return RTrim (this);} / / here is the independent function function LTrim (str) {var (I)!) {if (str.charAt (I)! = "& str.charAt (I)! =") break } str=str.substring (0djin1); return str;} function Trim (str) {return LTrim (RTrim (str));}

-dividing line-

Function trim (str) {/ / delete the spaces on the left and right ends return str.replace (/ (^\ s*) | (\ sroom$) / g, ");} function ltrim (str) {/ / delete the spaces on the left return str.replace (/ (^\ s*) / g,");} function rtrim (str) {/ delete the spaces on the right return str.replace (/ (\ sroom$) / g, "") } function checkSubmit () {if ("confirm (" are you sure you want to save the data? ") {var ab = document.getElementById (" name "). Value;var dj = document.getElementById (" dj "). Value;var xy = ab.replace (/ (^\ s *) | (\ ssaved $) / g,"); if (document.dwbzjlspb.action ="; document.dwbzjlspb.submit (); return true) } else {alert ("the unit name or the level to be declared cannot be empty!") ;}} else {return false;}}; the above is how to delete spaces before and after using regular expressions in javascript. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report