JavaScript 的 String strip() ?

发布于 2024-08-04 19:30:21 字数 76 浏览 7 评论 0原文

如何从字符串中去除前导和尾随空格?

例如,“dog” 应变为“dog”

How do I strip leading and trailing spaces from a string?

For example, " dog " should become "dog".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

寒冷纷飞旳雪 2024-08-11 19:30:21

使用这个:

if(typeof(String.prototype.trim) === "undefined")
{
    String.prototype.trim = function() 
    {
        return String(this).replace(/^\s+|\s+$/g, '');
    };
}

trim 函数现在将作为字符串上的一流函数使用。例如:

" dog".trim() === "dog" //true

编辑:采纳 JP 的建议,将正则表达式模式合并为一个。还根据克里斯托夫的建议添加了全局修饰符。

采用 Matthew Crumley 的想法,即在重新创建之前嗅探修剪功能。这样做是为了防止客户端使用的 JavaScript 版本更新,因此具有自己的本机修剪功能。

Use this:

if(typeof(String.prototype.trim) === "undefined")
{
    String.prototype.trim = function() 
    {
        return String(this).replace(/^\s+|\s+$/g, '');
    };
}

The trim function will now be available as a first-class function on your strings. For example:

" dog".trim() === "dog" //true

EDIT: Took J-P's suggestion to combine the regex patterns into one. Also added the global modifier per Christoph's suggestion.

Took Matthew Crumley's idea about sniffing on the trim function prior to recreating it. This is done in case the version of JavaScript used on the client is more recent and therefore has its own, native trim function.

蓝梦月影 2024-08-11 19:30:21

对于 jquery 用户,$.trim(s) 怎么样?

For jquery users, how about $.trim(s)

太阳哥哥 2024-08-11 19:30:21

Gumbo 已经在评论中指出了这一点,但这值得重复作为答案:trim() 方法是在 JavaScript 1.8.1 中添加的,并且受到所有现代浏览器的支持(Firefox 3.5+、IE 9、 Chrome 10、Safari 5.x),尽管 IE 8 及更早版本不支持。用法很简单:

 "  foo\n\t  ".trim() => "foo"

另请参阅:

Gumbo already noted this in a comment, but this bears repeating as an answer: the trim() method was added in JavaScript 1.8.1 and is supported by all modern browsers (Firefox 3.5+, IE 9, Chrome 10, Safari 5.x), although IE 8 and older do not support it. Usage is simple:

 "  foo\n\t  ".trim() => "foo"

See also:

乖乖兔^ω^ 2024-08-11 19:30:21

这是我使用的功能。

function trim(s){ 
  return ( s || '' ).replace( /^\s+|\s+$/g, '' ); 
}

Here's the function I use.

function trim(s){ 
  return ( s || '' ).replace( /^\s+|\s+$/g, '' ); 
}
你的笑 2024-08-11 19:30:21

来自 MDN 的更好的 polyfill支持删除 BOMNBSP

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  };
}

请记住,修改内置原型会带来性能损失(由于 JS 引擎对许多运行时优化进行了优化),并且在性能关键的情况下,您可能需要考虑定义 myTrimFunction(string) 的替代方案。话虽这么说,如果您的目标是没有本机 .trim() 支持的较旧环境,则可能需要处理更重要的性能问题。

A better polyfill from the MDN that supports removal of BOM and NBSP:

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  };
}

Bear in mind that modifying built-in prototypes comes with a performance hit (due to the JS engine bailing on a number of runtime optimizations), and in performance critical situations you may need to consider the alternative of defining myTrimFunction(string) instead. That being said, if you are targeting an older environment without native .trim() support, you are likely to have more important performance issues to deal with.

℉絮湮 2024-08-11 19:30:21

Steven Levithan 曾经写过如何实现 更快的 JavaScript 修剪。这绝对值得一看。

Steven Levithan once wrote about how to implement a Faster JavaScript Trim. It’s definitely worth a look.

要走干脆点 2024-08-11 19:30:21

如果您已经在使用 jQuery,那么您可能需要查看 jQuery.trim( ) 已随 jQuery 提供。

If you're already using jQuery, then you may want to have a look at jQuery.trim() which is already provided with jQuery.

ぃ弥猫深巷。 2024-08-11 19:30:21

如果您不是编写新代码来修剪字符串,而是查看调用“strip()”的现有代码并想知道它为什么不起作用,那么您可能需要检查它是否尝试包含类似prototypejs框架之类的内容,并确保它确实被加载。
该框架向所有 String 对象添加了一个 strip 函数,但是如果您升级了它并且您的网页仍然引用旧的 .js 文件,那么它当然无法工作。

If, rather than writing new code to trim a string, you're looking at existing code that calls "strip()" and wondering why it isn't working, you might want to check whether it attempts to include something like the prototypejs framework, and make sure it's actually getting loaded.
That framework adds a strip function to all String objects, but if e.g. you upgraded it and your web pages are still referring to the old .js file it'll of course not work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文