仅使用 jQuery 从字符串的末尾修剪空格 [英] Trimming whitespace from the END of a string only, with jQuery

查看:23
本文介绍了仅使用 jQuery 从字符串的末尾修剪空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 jQuery $.trim() 函数,但我需要的是一种仅从字符串的结尾(而不是开头)修剪空格的方法.

I know of the jQuery $.trim() function, but what I need is a way to trim whitespace from the END of a string only, and NOT the beginning too.

所以

  str ="     this is a string     ";

会变成

  str ="     this is a string";

有什么建议吗?

谢谢!

推荐答案

您可以使用正则表达式:

You can use a regex:

str = str.replace(/\s*$/,"");

它表示用空字符串替换字符串末尾的所有空格.

It says replace all whitespace at the end of the string with an empty string.

细分:

  • \s* : 任意数量的空格
  • $ : 字符串的结尾
  • \s* : Any number of spaces
  • $ : The end of the string

关于正则表达式的更多信息:

More on regular expressions:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

这篇关于仅使用 jQuery 从字符串的末尾修剪空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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