33cent
33cent

Reputation: 41

jquery: auto-create url from title

i'm looking for jQuery code which will suggest me a URL for new CMS article, based on the Title of article.

Example:

I wrote title: The sky is blue

jQuery autogenerated URL: /the-sky-is-blue/

Upvotes: 0

Views: 276

Answers (2)

mike
mike

Reputation: 4433

function slugify(str){
    return str.replace(/\s+/g,'-').replace(/[^a-zA-Z0-9\-]/g,'').toLowerCase();
}

var mySlug = slugify("testing the slugify function"); // testing-the-slugify-function

Upvotes: 2

Joel
Joel

Reputation: 19368

One way without jquery:

var title = text.replace(/\s+/g, '-').toLowerCase();

Upvotes: 0

Related Questions