Categories: Snippets

Javascript Slugify

function slugify(text)
{
  return text.toString().toLowerCase()
    .replace(/\s+/g, '-')           // Replace spaces with -
    .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
    .replace(/\-\-+/g, '-')         // Replace multiple - with single -
    .replace(/^-+/, '')             // Trim - from start of text
    .replace(/-+$/, '');            // Trim - from end of text
}

 

Share

Recent Posts

Selecting Elements in jQuery (jQuery Selectors)

To collect a group of elements, we pass the selector to the jQuery function using…

5 years ago

jQuery library Introduction

jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is…

5 years ago

Adjust li width fit with ul width of the outer div/body

HTML:- [crayon-663d5f1f37401445080754/] CSS:- [crayon-663d5f1f3740b070466081/] JavaScript:- [crayon-663d5f1f3740f056392309/]  

5 years ago

Customize Input Box using jQuery

Prevent users typing special characters in text box, textarea, etc. [crayon-663d5f1f37630275835228/] ^[a-zA-Z]+$ Alphabets ^[a-zA-Z\s]+$ Alphabets…

5 years ago

JavaScript simple form validation with example

Any interactive web site has form input - a place where the users input different…

5 years ago

Clear the browser cache of CSS or JavaScript Using Javascript

[crayon-663d5f1f37b2f930272397/]  

5 years ago