Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  148] [ 6]  / answers: 1 / hits: 26611  / 13 Years ago, wed, april 27, 2011, 12:00:00

I want to remove space in the beggining of each line.



I have data in each line with a set of spaces in the beginning so data appears in the middle, I want to remove spaces in the beginning of each line.



tmp = tmp.replace(/(<([^>]+)>)/g,)


How can I add the ^s condition into that replace()?


More From » javascript

 Answers
7

To remove all leading spaces:



str = str.replace(/^ +/gm, '');


The regex is quite simple - one or more spaces at the start. The more interesting bits are the flags - /g (global) to replace all matches and not just the first, and /m (multiline) so that the caret matches the beginning of each line, and not just the beginning of the string.



Working example: http://jsbin.com/oyeci4


[#92542] Tuesday, April 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
parker

Total Points: 259
Total Questions: 109
Total Answers: 97

Location: Zambia
Member since Thu, Jun 25, 2020
4 Years ago
;