views:

832

answers:

1

I need to reformat a string using jquery or standard javascript

Lets say we have

Sonic Free Games

I want to convert it to

sonic-free-games

So white spaces replaced with dashes and all letters converted to small letters

Any help on this please ?

+8  A: 

Just use the String replace and toLowerCase methods, for example:

var str = "Sonic Free Games";
str.replace(/\s+/g, '-').toLowerCase();
// "sonic-free-games"

Notice the g flag on the RegExp, it will make the replacement globally within the string, if it's not used, only the first occurrence will be replaced, and also, that RegExp will match one or more white-space characters.

CMS
working perfect .. thank you
NetCaster
You're welcome NetCaster!
CMS