jquery - Javascript replace utf characters in string -
i want after type title of post automatically take value , create slug. code works fine english latin characters problem when type characters 'čćšđž'. code replace first type of characters in string if character repeated problem. so, testing purpose title 'šžđčćžđš čćšđžčćšđž čćšđžčć ćčšđžšžčćšđ ćčšžčć' converted slug 'szdcc'.
this jquery code:
$('input[name=title]').on('blur', function() { var slugelement = $('input[name=slug]'); if(slugelement.val()) { return; } slugelement.val(this.value.tolowercase().replace('ž', 'z').replace('č','c').replace('š', 's').replace('ć', 'c').replace('đ', 'd').replace(/[^a-z0-9-]+/g, '-').replace(/^-+|-+$/g, '')); });
how solve problems? possible few characters put in same replace() function?
replace() replaces first occurrence unless regex used global modifier. need change them regular expression.
replace(/ž/g, "z")
as far know, not possible use single replace() in case. if concerned chaining bunch of .replace() together, might better off writing custom code replace these characters.
var newstr = ""; (var = 0; < str.length; i++) { var c = str.charat(i); switch (c) { case "ž": newstr += "z"; break; default: newstr += c; break; } }
Comments
Post a Comment