In javascript, the regular expression is used to delete leading and trailing spaces.

Source: Internet
Author: User
Tags rtrim

In javascript, the regular expression is used to delete leading and trailing spaces.

Remove leading space

Copy codeThe Code is as follows:
Str = str. replace (/^ \ s + | \ s + $/g ,'');

The js Regular Expression deletes 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 in the second line.

^ Start row

\ S *
Match All spaces before the character. The greedy mode is repeated.

(.*?)

Capture group, barely match any character repeatedly, that is, the character we finally need (after removing the leading and trailing spaces), this is not very easy to understand (I think)

First, I originally thought that the first character should not be written as a space ([^ \ s +]) in the capture group, however, this is completely unnecessary because \ s * in front of the capture group can capture all leading space characters, in your opinion, the character starting range of the capture group is different from that of the regular expression ~ I can't explain it clearly.

Second: Where? Its role is to barely repeat the previous characters. What does it mean? That is, if I use (. * a) to match the aaaaaaa string, the result is the (aaaaaaa) Source string. This is called greedy mode. If I use (.*? A) To match aaaaaaa, he will first match the first a, then the second a, and then the third ....... This is called stubborn pattern matching. In some cases, it is also called lazy pattern matching. A more popular one (people like the plain explanation, huh, huh) means that the former matches as many characters as possible from the back to the back, while the latter matches from the back to the back.

Third: Do we need to care about the spaces behind the capture group? Because ". "It can also match spaces. Previously, I also considered this issue to be wasted most of the time. in fact, this is the same as considering whether to exclude spaces in front of the capture group. The \ s * behind it has already been processed by us.

\ S * space after matching characters

------------------- Split line -------------------

You can customize three trim () functions to filter spaces between the left and right sides of a string.

// Js remove space function // Add three members to the string class here. prototype. trim = function () {return Trim (this);} String. prototype. LTrim = function () {return LTrim (this);} String. prototype. RTrim = function () {return RTrim (this) ;}// the independent function LTrim (str) {var I; for (I = 0; I <str. length; I ++) {if (str. charAt (I )! = "" & Str. charAt (I )! = ") Break;} str = str. substring (I, str. length); return str;} function RTrim (str) {var I; for (I = str. length-1; I> = 0; I-) {if (str. charAt (I )! = "" & Str. charAt (I )! = "") Break;} str = str. substring (0, I + 1); return str;} function Trim (str) {return LTrim (RTrim (str ));}

------------------- Split line -------------------

<Script type = "text/javascript"> function trim (str) {// Delete the leading and trailing spaces return str. replace (/(^ \ s *) | (\ s * $)/g, "");} function ltrim (str) {// Delete the left space return str. replace (/(^ \ s *)/g, ");} function rtrim (str) {// Delete the right space return str. replace (/(\ s * $)/g, ") ;}</script> 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 *) | (\ s * $)/g, ""); if (xy! = "& Dj! = ") {Document. dwbzjlspb. action = ""; document. dwbzjlspb. submit (); return true;} else {alert ("the organization name or level to be declared cannot be blank! ");}} Else {return false ;}};

The above content shares with you the method of deleting spaces before and after the js regular expression. I hope you 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.