admin管理员组文章数量:1426491
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.
So
str =" this is a string ";
would become
str =" this is a string";
Any suggestions?
Thanks!
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.
So
str =" this is a string ";
would become
str =" this is a string";
Any suggestions?
Thanks!
Share Improve this question asked Jul 30, 2013 at 4:14 Sharon SSharon S 3652 gold badges3 silver badges10 bronze badges2 Answers
Reset to default 42You can use a regex:
str = str.replace(/\s*$/,"");
It says replace all whitespace at the end of the string with an empty string.
Breakdown:
\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
For some browsers you can use:
str = str.trimRight();
or
str = str.trimEnd();
If you want total browser coverage, use regex.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd
本文标签: javascriptTrimming whitespace from the END of a string onlywith jQueryStack Overflow
版权声明:本文标题:javascript - Trimming whitespace from the END of a string only, with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737632282a1999659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论