JavaScript replace () Method: remove empty space only at the end and at the beginning of a line

Possible duplicate:
How to trim a string in javascript?

Using the replace method in javascript, I am trying to remove the empty space between the beginning and end of a line:

Here is my code:

Any idea how I should achieve the result?

input -> " first second ".replace(/[^\s|\s$]/g, ''); // " " output -> "first second" 
+6
source share
6 answers

This is called cropping.

You need brackets instead of brackets in the regular expression, as well as a multiplier in the white space specifier to match multiple spaces:

 var s = " first second ".replace(/(^\s+|\s+$)/g, ''); 

Demo: http://jsfiddle.net/Guffa/N7xxt/

+4
source

Add this at the beginning of your script:

 // Add ECMA262-5 string trim if not supported natively // if (!('trim' in String.prototype)) { String.prototype.trim= function() { return this.replace(/^\s+/, '').replace(/\s+$/, ''); }; } 

Then use yourString.trim() to remove the spaces at the beginning and end of the line.

+3
source

You must use parentheses () to indicate that you want to combine either the left or right side of the "|" character, not square brackets. The square brackets actually correspond to character sets (ie [Grapes] will correspond to a single instance of "g", "r", "a", "p", "e" or "s", whereas (grapes | apples ) will correspond to "grapes" or "apples").

In addition, another thing that you are missing is the indication of quantity. In other words, once you match a space (\ s), how many places should it look for? In your example, this matches only one space. You probably want to combine as many consecutive spaces as there are left and right of the line. To do this, you need to add * (a match of zero or more) or + (a match of one or more) immediately after the \ s character.

So, to rewrite your regex:

 var input = " first second "; var trimmed = input.replace(/(^\s+|\s+$)/g, ''); console.log(trimmed); 

You can copy and paste these lines into the JavaScript console to see that you get the desired result. In this regular expression, the regular expression "matches one or more space characters from the beginning of a line or one or more space characters, followed by the end of a line." Then, the replace function accepts this correspondence and replaces it with ".".

+1
source

Why is a method needed? Just use cropping. IE From jQuery library

http://api.jquery.com/jQuery.trim/

0
source

You can use jQuery trim.

 <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <pre id="original"></pre> <pre id="trimmed"></pre> <script> var str = " first second "; $("#original").html("Original String: '" + str + "'"); $("#trimmed").html("After trimed: '" + $.trim(str) + "'"); </script> </body> </html> 
0
source

Change litle for your template:

 (^\s+|\s+$) 

and then

 var string = " test "; string.replace(/(^\s+|\s+$)/gm, ''); 

must work; -)

0
source

Source: https://habr.com/ru/post/923472/


All Articles