简体   繁体   English

如何删除句号和前一个单词之间的空格

[英]How to remove space between period and the prior word

I have the following string:我有以下字符串:

"I like dogs ." 

I want to make it to this:我想做到这一点:

"I like dogs."

How can I do this using regex?我怎样才能使用正则表达式做到这一点? I tried to do the following but it didn't work:我尝试执行以下操作,但没有奏效:

  var str = " I like dogs . ";
  str = str.replace(/\s+$/g, "");

There's no need to make it so complicated.没有必要让它变得如此复杂。

> "I like dogs .".replace(/\s+\./, ".");
"I like dogs."

Go for the easy approach, unless you have some odd layout elsewhere.选择简单的方法,除非您在其他地方有一些奇怪的布局。

 str = "I like dogs ."; str = str.replace(/ \\./g, '.'); console.log(str); //output: I like dogs.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM