我正在尝试使用以下代码从 textarea 输入框中删除所有前导空格:
replaceString = replaceString.replace(/^\s+|^\t+|\t+|\s+$/g, "");
当我这样做时,我从
.map_image #map_link_0 { width: 40px; height: 42px; top: 11px; left: 11px; }
.map_image #map_link_1 { width: 47px; height: 42px; top: 62px; left: 19px; }
到
.map_image #map_link_0 { width: 40px; height: 42px; top: 11px; left: 11px; }
.map_image #map_link_1 { width: 47px; height: 42px; top: 62px; left: 19px; }
我做错了什么?
最佳答案
表达式默认不考虑多行,所以 ^
只匹配你输入的开头。您可以通过添加多行 /m
标志(总共 /gm
)来解决此问题。
来自 Mozilla's JS RE docs :
^
Matches beginning of input. If the multiline flag is set to true, also matches immediately after a line break character.
$
Matches end of input. If the multiline flag is set to true, also matches immediately before a line break character.
关于javascript - 似乎无法 trim 所有前导空格 - JavaScript 或 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31076637/