Brian Workman
Tera Contributor

This is a very straight-forward script to take a person's name, strip out accents and special characters and spaces for the purpose of using in a user id or an email address.

// Caveat : I am 💯% mixing names and genders around in my example
// just to get names with accents, etc.
//
// This is just for demonstration and is not an attempt at representing
// real name combinations

var firstname = "Frédéric-Noël" ;
var lastname = "Jokūbas_KŠthe Smith"

var result = {
    first: normalise( firstname ),
    last : normalise( lastname )
} ;

console.info(result) ;

function normalise (input) {
    return input
        .toString()                         // Just in case, you know what I mean
        .normalize('NFD')                   // Canonical Decomposition
        .replace(/[\u0300-\u036f]/g, "")    // Remove diacritics
        .replace(/\s/g,"")                  // Strip spaces
        .replace(/[\W_]+/g,"")              // Strip all non-word characters and underlines
        ;
}

// [object Object] {
//     first: "FredericNoel",
//     last: "JokubasKStheSmith"
//   }

 

 

Comments
Charlotte Pakes
Tera Guru

Thanks very much, this was exactly what I needed.

Community Alums
Not applicable

Hi 

I have solved above  problem like below code 

switch (field) {
                case "name":
                case "managed_by":
                 {
                    var keyCapry = capry_util.serviceNowToCapryFieldTransform(key);
                    var nonEngV = value.toString(); // Just in case, you know what I mean
                    var nonEngV1 = nonEngV.normalize('NFD'); // Canonical Decomposition
                    var ss = nonEngV1.replace(/[\u0300-\u036f]/g""); // Remove diacritics
                    jsonObj[keyCapry] = ss;
                    break;
                }
working as expected but if we we can update them as it follows?

ä ---> ae
ë ---> e
ï ---> i
ö ---> oe
ü ---> ue
ÿ ---> y
ß --> ss

 please help me how to translation of the umlauts like above.

 

Thanks & Regards,

Bandi

Ark4
Tera Contributor

Hi,

This script only works on background & not on the script includes.

Did you find a way around this?

Brian Workman
Tera Contributor

Do you mean mine, or "Community Alums"'s? Because mine definitely works in a script include. There is legit nothing special about. It is as vanilla JS as it comes.

Brian Workman
Tera Contributor

"Community Alum"'s question has to do with transliteration. And I have done some research and have found the answer.

 

You can include speakingurl as a script include from its CDN, either the full one or the minified one. Perform the transliteration before you perform the normalisation.

Ark4
Tera Contributor

My Bad, it seems to be working. Need to pass the inputs as String. 

Version history
Last update:
‎07-22-2021 08:11 AM
Updated by: