简体   繁体   中英

Remove white space from beginning and end of LINES

I am making a website where you can browse software licenses for your project. I am currently having trouble removing spaces at the beginning of the lines when you copy a license.

screenshot: screenshot

i have been trying various regular expressions, but none of them seem to work. Any help would be appreciated. Thanks.

You can use trim for such things. http://www.w3schools.com/jsref/jsref_trim_string.asp

To remove whitespace from the beginning of a line, use

.replace(/^[^\S\r\n]+/gm, "")

Pattern details:

  • ^ - start of a line (since multiline modifier /m is used)
  • [^\\S\\r\\n] - matches any character other than a nonwhitespace, carriage return and line feed characters. + makes it match one or more occurrences.

try this

var text = "          this is a string         \n"+
       "    \t    with a much of new lines     \n";
text.replace(/^\s*/gm, '');

Regular expression to remove space in the beginning of each line?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM