Javascript 和正则表达式:删除字符串中最后一个单词后的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11306008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:23:22  来源:igfitidea点击:

Javascript and regex: remove space after the last word in a string

javascriptregex

提问by Milo?

I have a string like that:

我有一个这样的字符串:

var str = 'aaaaaa, bbbbbb, ccccc, ddddddd, eeeeee ';

My goal is to delete the last space in the string. I would use,

我的目标是删除字符串中的最后一个空格。我会用,

str.split(0,1);

But if there is no space after the last character in the string, this will delete the last character of the string instead.

但是如果字符串的最后一个字符后没有空格,这将删除字符串的最后一个字符。

I would like to use

我想用

str.replace("regex",'');

I am beginner in RegEx, any help is appreciated.

我是 RegEx 的初学者,任何帮助表示赞赏。

Thank you very much.

非常感谢。

回答by Francis Avila

Do a google search for "javascript trim" and you will find manydifferent solutions.

用谷歌搜索“javascript trim”,你会发现许多不同的解决方案。

Here is a simple one:

这是一个简单的:

trimmedstr = str.replace(/\s+$/, '');

回答by Igor Chubin

When you need to remove all spaces at the end:

当您需要删除末尾的所有空格时:

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

When you need to remove one space at the end:

当您需要在末尾删除一个空格时:

str.replace(/\s?$/,'');

\smeans not only space but space-like characters; for example tab.

\s不仅表示空格,还表示类似空格的字符;例如标签。

If you use jQuery, you can use the trimfunction also:

如果您使用 jQuery,您也可以使用该trim函数:

str = $.trim(str);

But trimremoves spaces not only at the end of the string, at the beginning also.

trim不仅在字符串的末尾删除空格,也在开头删除。

回答by Shiplu Mokaddim

Seems you need a trimRightfunction. its not available until Javascript 1.8.1. Before that you can use prototyping techniques.

看来你需要一个trimRight函数。它在 Javascript 1.8.1 之前不可用。在此之前,您可以使用原型技术。

 String.prototype.trimRight=function(){return this.replace(/\s+$/,'');}
 // Now call it on any string.
 var a = "a string ";
 a = a.trimRight();

See more on Trim string in JavaScript?And the compatibility list

在 JavaScript 中查看有关修剪字符串的更多信息和兼容性列表

回答by nhahtdh

You can use this code to remove a single trailing space:

您可以使用此代码删除单个尾随空格:

.replace(/ $/, "");

To remove all trailing spaces:

要删除所有尾随空格:

.replace(/ +$/, "");

The $matches the end of input in normal mode (it matches the end of a line in multiline mode).

$在正常模式下(它多行模式的线的端部相匹配)输入的端部相匹配。

回答by Scott Stevens

Try the regex ( +)$since $in regex matches the end of the string. This will strip all whitespace from the end of the string.

尝试使用正则表达式,( +)$因为$在正则表达式中匹配字符串的结尾。这将从字符串的末尾去除所有空格。

Some programs have a stripfunction to do the same, I do not believe the stadard Javascript library has this functionality.

有些程序有一个strip功能可以做同样的事情,我不相信标准的 Javascript 库有这个功能。

Regex Reference Sheet

正则表达式参考表

回答by Purvesh Tejani

Working example:

工作示例:

   var str  = "Hello World  ";
   var ans = str.replace(/(^[\s]+|[\s]+$)/g, '');

   alert(str.length+" "+ ans.length);