javascript - 似乎无法 trim 所有前导空格 - JavaScript 或 jQuery

标签 javascript jquery regex string trim

我正在尝试使用以下代码从 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; }

我做错了什么?

JSFiddle

最佳答案

表达式默认不考虑多行,所以 ^ 只匹配你输入的开头。您可以通过添加多行 /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/

相关文章:

javascript - 仅针对特定 div/s 运行 javascript

javascript - Bootstrap Datepicker定位不起作用

javascript - 指令作为 Bootstrap ui 模式中的类不编译

javascript - 关于使用 Javascript/Jquery 构建复杂表单的建议

javascript - 从 PHP 填充选择下拉列表

javascript - jquery grep,使用正则表达式过滤

javascript - 附加组件 SDK Mozilla Firefox currentURI

javascript - Jquery fadeOut 函数有时无法正常工作

c++ -::std::regex_replace with syntax flag icase on Windows (VS2013 Update 4, VS2015 Update 3) 不匹配使用字符范围

javascript - 如何使用 JavaScript 删除 html 标签只保留 BR、B 和 I 标签?