Posted By

Activetuts on 01/13/11


Tagged


Versions (?)

Who likes this?

18 people have marked this snippet as a favorite

chus
elbjoern
EderChrono
toxin20
liakwee
Demian
ichnoweb
kure69
AndrewPotter
JaaQ
im3der
kojok
elderhsouza
chrisaiv
jbyerson
shalomfriss
coutlasssupreme
flashdeveloper


Slugify


 / Published in: ActionScript 3
 

URL: http://enva.to/e4ig6z

Wikipedia describes a slug as "part of a URL which identifies a page using human-readable keywords". A slug is usually a few words with each word separated by a delimiting character, usually an underscore or hyphen. A slugified version of the string "10 Tips to a Better Life!" would be "10-tips-to-a-better-life".

This function takes an arbitrary string and converts in for use as a slug. We use two regular expressions to accomplish this, the first is used to remove any character which is not a word character, space or hyphen, the second replaces any spaces with a single hyphen. Finally we convert the string to lowercase. This returns a new string, leaving the input string unchanged.

  1. function slugify(string:String):String
  2. {
  3. const pattern1:RegExp = /[^\w- ]/g; // Matches anything except word characters, space and -
  4. const pattern2:RegExp = / +/g; // Matches one or more space characters
  5. var s:String = string;
  6. return s.replace(pattern1, "").replace(pattern2, "-").toLowerCase();
  7. }

Report this snippet  

You need to login to post a comment.