Question

I am writing a function to make slug from input.

var vslug = function (str) {
  str = str.replace(/^\s+|\s+$/g, '');
  str = str.toLowerCase();
  var vregex = /(?:\.([^.]+))?$/;
  var filename  = str.replace(vregex.exec(str)[0],'');
  var extension = vregex.exec(str)[1];
  var from = "àáäâèéëêìíïîıòóöôùúüûñçşğ·/,:;";
  var to   = "aaaaeeeeiiiiioooouuuuncsg_____";
  for (var i = 0; i < from.length; i++) {
    console.log('before ' + str);
    str = filename.replace(new RegExp(from[i], 'g'), to[i]);
    console.log('after ' + str);
  }
  str = str.replace(/[^a-z0-9 _-]/g, '')
  .replace(/\s+/g, '_')
  .replace(/-+/g, '_');
  if (typeof extension !== "undefined") {
    return str+'.'+extension;
  } else {
    return str;
  }
};

I can't make this part - I gone blind. Any help is appreciated..

  var from = "àáäâèéëêìíïîıòóöôùúüûñçşğ·/,:;";
  var to   = "aaaaeeeeiiiiioooouuuuncsg_____";
  for (var i = 0; i < from.length; i++) {
    console.log('before ' + str);
    str = filename.replace(new RegExp(from[i], 'g'), to[i]);
    console.log('after ' + str);
  }
Était-ce utile?

La solution

filename is not changed - the variable names the same string, and the string cannot be modified. As such, each loop starts working on the original string again when it uses filename.replace...

Instead, eliminate filename (or integrate it fully) and use str = str.replace..

str = str.replace(vregex.exec(str)[0],'');
for (var i = 0; i < from.length; i++) {
  str = str.replace(new RegExp(from[i], 'g'), to[i]);
  // ^-- next loop gets new value
}

(Also, this could be handled with a replacement function and a map instead of n-loops and there might be a Unicode library for JavaScript available..)


An approach using a map and a replacement function might look like:

// Specify map somewhere reusable; can be built from paired arrays for simplicity.
var replacements = {"à":"a", "á":"a", .. ";":"_"}
// Object.keys is ES5, shim as needed. e.g. result: [à;á..]
var alternation = "[" + Object.keys(replacements).join("") + "]"
// This regex will match all characters we are trying to match.
var regex = new Regex(alternation, "g")

str = str.replace(regex, function (m) { 
    var r = replacements[m]
    return r || m        
})

See String.replace(regex, function)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top