正则表达式删除每行开头的空格? [英] Regular expression to remove space in the beginning of each line?

查看:781
本文介绍了正则表达式删除每行开头的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除每行开头的空格。

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,"")

如何添加 ^ \s 条件为 replace()

推荐答案

删除所有前导空格:

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

正则表达式非常简单 - 一开始就有一个或多个空格。更有趣的是标志 - / g (全局)来替换所有匹配而不仅仅是第一个,而 / m (multiline)使插入符号与每行的开头匹配,而不仅仅是字符串的开头。

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.

工作示例: http://jsbin.com/oyeci4

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

这篇关于正则表达式删除每行开头的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆