JavaScript removing trailing white-space and periods from string

I am attempting to remove all trailing white-space and periods from a string so that if I took either of the following examples:

var string = "  ..  bob is a string .";


var string = " .  bob is a string . ..";

They would end up as:

"bob is a string"

I know diddly squat about regex but I found a function to remove trailing white-space here:

str.replace(/^\s+|\s+$/g, "");

I tried modifying to include periods however it still only removes trailing white-space characters:

str.replace(/^[\s+\.]|[\s+\.]$/g, "");

Can anyone tell me how this is done and perhaps explain the regular expression used to me?


Your regex is almost right, you just need to put the quantifier (+) outside of the character class ([]):

var str = " .  bob is a string . ..";
str.replace(/^[.\s]+|[.\s]+$/g, "");
//"bob is a string"

Need Your Help

How to return json date from MVC4 controller in ISO format json serialization

I tried tro return date in ISO format using Json.Net from ASP.NET MVC4 controller

MVC scheduled mail sending email scheduled-tasks ncron

I have got an ASP.NET MVC 4 application, and I want it to send a report e-mail every week. I've read about Quartz.NET, but it's too powerful for this easy task. Now I'm trying to use NCron, but it

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.