hey everyone. I have a bit of a problem - i'm using javascript to automatically generate web-friendly urls from the titles, but i've run into some issues with special characters This is the code: function setAlias(objvalue){ document.newpage.newpagealias.value = makesafe(objvalue); } function makesafe(text){ text = text.replace(" / ","_"); text = text.replace("'",""); text = text.replace("\"",""); text = text.replace("/","_"); text = text.replace("'","_"); text = text.replace(" - ","_"); text = text.replace(" ","_"); text = text.replace("è","c"); text = text.replace("Å¡","s"); text = text.replace("ž","z"); text = text.replace("È","C"); text = text.replace("Å ","S"); text = text.replace("Ž","Z"); text = text.replace( "&", "and"); text = text.replace( "%", "Percent"); text = alphanumeric(text); text = text.replace("____","_"); text = text.replace("___","_"); text = text.replace("__","_"); text = text.toLowerCase(); return text; } function alphanumeric(userval) { for(var j=0; j<userval.length; j++) { var eachChar = userval.charAt(j); var hh = eachChar.charCodeAt(0); if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || (eachChar =="_") || (eachChar =="-")) { // } else { userval = userval.replace(eachChar,"_"); } } return userval; } Code (markup): The two input fields that are connected here are the following two: <input type="text" class="text" name="newpagetitle" value="" size="56" maxlength="255" onchange="setAlias(this.value)" /> and <input type="text" class="text" name="newpagealias" value="" size="16" maxlength="50" /> Code (markup): basically it should convert all ÄšžČŠŽ into cszcsz but it does this only for the first of them, then turns the rest into _ So my question is basically... Does anyone know what would need changing here so that this would work in converting all special characters into regular letters and ... does anyone know if it's also possible to remove the ending underscore (example from "word_" to "word")? Thanks so much!
Keep in mind that the javascript replace() function uses regular expressions, it is not a simple "replace(this, withThat)" type of function like you would find in VB or C#. I also see a lot of messy and redundant things in your code. For example, trying to replace four underscores, then three, then two with a single underscore. A loop would be able to strip out multiple underscores more effectively. And instead of checking for uppercase and lowercase special characters, then converting the string to lowercase as the last step, it is better to convert the string to lowercase first and then check for lowercase special characters, because the toLowerCase() function will convert ÄŒ to Ä, Å to Å¡, and Ž to ž for you. Here is how I think it should probably be implemented, although others might recommend some improvements. Get rid of the alphanumeric() function since its purpose is handled in the revised function below: function makesafe(text) { text = text.toLowerCase(); text = text.replace(/Ä/g, "c"); text = text.replace(/Å¡/g, "s"); text = text.replace(/ž/g, "z"); text = text.replace(/&/g, " and "); text = text.replace(/%/g, " percent "); text = text.replace(/\W/g, "_"); while (text.indexOf("__") >= 0) { text = text.replace(/__/g, "_"); } if ((text.length > 0) && (text.lastIndexOf("_") == (text.length - 1))) { text = text.substr(0, text.length - 1); } return text; } Code (markup): To test it thoroughly, use the following input string: The above test string will be converted to: