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 use regular expressions to delete spaces in strings in javascript

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

Share

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

This article introduces how to use regular expressions in javascript to delete spaces in strings. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

The code is as follows:

Var str= "yexj00"

Str=str.replace (/ ^ s* | scars racing gpencil')

Alert (str)

Example

Imitate RTrim:

The copy code is as follows:

Var str= "yexj00"

Str=str.replace (/ swords, games, gamma, etc.)

Alert (str)

Example

The code and the test code are as follows:

The copy code is as follows:

/ / removes white space characters on both sides of the string.

Function trim (str) {

Return str.replace (/ ^ s + | scars racing gpencil')

}

/ / removes the white space character on the left side of the string.

Function ltrim (str) {

Return str.replace (/ ^ swatches / gjinghee')

}

/ / removes the white space character on the right side of the string.

Function rtrim (str) {

Return str.replace (/ swords, games, gamma, etc.)

}

/ / the following is the test code

Var trimTest = "123456789"

/ / there is a space before and after each.

[xss_clean] ('length:'+trimTest.length+'')

/ / before use

[xss_clean] ('ltrim length:'+ltrim (trimTest) .length +')

/ / after using ltrim

[xss_clean] ('rtrim length:'+rtrim (trimTest) .length +')

/ / after using rtrim

[xss_clean] ('trim length:'+trim (trimTest) .length +')

/ / after using trim

The test results are as follows:

The copy code is as follows:

Length:11

Ltrim length:10

Rtrim length:10

Trim length:9

Use js regular expressions to remove spaces before and after a string

The copy code is as follows:

String.prototype.trim=function () {

Var reSpace=/ ^\ s * (. *?)\ skeeper /

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.

The copy code is as follows:

/ / js space removal function

/ / add three members to the string class here

String.prototype.Trim = function () {return Trim (this);}

String.prototype.LTrim = function () {return LTrim (this);}

String.prototype.RTrim = function () {return RTrim (this);}

/ / here is an independent function

Function LTrim (str)

{

Var i

For (iphero-position i0 -)

{

If (str.charAt (I)! = "& & str.charAt (I)! =") break

}

Str=str.substring (0sperm iTunes 1)

Return str

}

Function Trim (str)

{

Return LTrim (RTrim (str))

}

-dividing line-

The copy code is as follows:

Function trim (str) {/ / remove the spaces at the left and right ends

Return str.replace (/ (^\ s *) | (\ s $) / g, "")

}

Function ltrim (str) {/ / remove the space on the left

Return str.replace (/ (^\ s*) / g, ")

}

Function rtrim (str) {/ / remove the space on the right

Return str.replace (/ (\ s $) / g, "")

}

Function checkSubmit () {

If (confirm ("are you sure you want to save data?") {

Var ab = document.getElementById ("name") .value

Var dj = document.getElementById ("dj") .value

Var xy = ab.replace (/ (^\ s *) | (\ s $) / g, ")

If (xylic = "" & & djacks = "") {

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

}

}

On how to use regular expressions in javascript to delete spaces in the string to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.

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