tags:

views:

392

answers:

1

Hello all.

I need a method in JavaScript to escape all characters which are not ( a-z / A-Z / 0-9 / - / _ )

If the character is ø it should be replaced with oe, if it's å then replaced with aa, and more.... if characters are not on the list, they should be replaced with an underscore.

If there are 2 underscores in a row ( __ ) they should be replaced with a single underscore.

I need this done in JavaScript and/or PHP.

+4  A: 
String.prototype.slugify = function(){
    return this.replace('ø','oe').replace('å','aa').replace(/\W/gi,'_').replace(/_+/g,'_');
}
var x = 'sdfkjshødfjsåkdhf#@$%#$Tdkfsdfxzhfjkasd23hj4rlsdf9';
x.slugify();

Add as many rules as you'd like following the .replace('search','replace') pattern. Make sure that you finish it with .replace(/\W/gi,'_').replace(/_+/,'_'), which converts . Also ensure you serve it up in UTF-8 to accommodate the special characters like ø.

An alternate version, suggested by Strager:

String.prototype.slugify = function(){
    var replacements = {
     'ø': 'oe',
     'å': 'aa'
    }
    var ret = this;
    for(key in replacements) ret = ret.replace(key, replacements[key]);
    return ret.replace(/\W/gi,'_').replace(/_+/g,'_');
}

This version is certainly more flexible and maintainable. I'd use this one, though I'm keeping the previous iteration for posterity. Great idea, Strager!

cpharmston
Using an object may be more convenient if there are a lot of special replacements to be made.
strager
Also, I think you may want to add the /g flag to the underscore-collapse regexp.
strager
When creating new string manipulation functions, I prefer to extend the `String` object to preserve chainability, etc. Good catch on the /g flag, my mistake! Edited.
cpharmston
By object I meant iterating through an object for replacements, like: `{ 'ø': 'oe', 'å': 'aa' }`
strager
Ah, great call with the dictionary! Answer updated.
cpharmston
nice, its help just only one problem. var replacements = { 'ø': 'oe', 'Ø': 'OE', 'å': 'aa', 'Å': 'AA', 'æ': 'ae', 'Æ': 'AE' } var ret = str; for(key in replacements) { ret = ret.replace(key, replacements[key]); } document.getElementById( id ).value = ret.replace(/\W/gi,'_').replace(/_+/g,'_');when i run it, whit this ( øøø ) its make oe_ its replace to oeoeoe and not oe_ :/ pls help here to.
NeoNmaN
You need to use http://pastebin.com - it's almost impossible to read code with linebreaks stripped like this.
cpharmston
The issue is `.replace` doesn't replace all occurances, just the first. Either rewrite as `.replace(new RegExp(key, 'g'), replacements[key])` or use a loop. (Latter requires more code but is safer.)
strager