2017-12-28 14 views
2

Hier habe ich die folgende Funktion, um die Zeichenfolge in eine Schnecke zu konvertieren, um SEO freundliche URL zu machen.Wie konvertiert man Unicode-String mit jQuery in das richtige Zeichen?

stringToSlug: function (title) { 
    return title.toLowerCase().trim() 
     .replace(/\s+/g, '-')   // Replace spaces with - 
     .replace(/&/g, '-and-')   // Replace & with 'and' 
     .replace(/[^\w\-]+/g, '')  // Remove all non-word chars 
     .replace(/\-\-+/g, '-')   // Replace multiple - with single - 
    } 

var title1 = 'Maoist Centre adamant on PM or party chair’s post'; 
 
function stringToSlug1 (title) { 
 
    return title.toLowerCase().trim() 
 
    .replace(/\s+/g, '-')   // Replace spaces with - 
 
    .replace(/&/g, '-and-')   // Replace & with 'and' 
 
    .replace(/[^\w\-]+/g, '')  // Remove all non-word chars 
 
    .replace(/\-\-+/g, '-')   // Replace multiple - with single - 
 
} 
 
console.log(stringToSlug1(title1)); 
 

 
var title2 = 'घर-घरमा ग्यास पाइपः कार्यान्वयनको जिम्मा ओलीकै काँधमा !'; 
 

 
function stringToSlug2 (title) { 
 
    return title.toLowerCase().trim() 
 
    .replace(/\s+/g, '-')   // Replace spaces with - 
 
    .replace(/&/g, '-and-')   // Replace & with 'and' 
 
    .replace(/[^\w\-]+/g, '')  // Remove all non-word chars 
 
    .replace(/\-\-+/g, '-')   // Replace multiple - with single - 
 
} 
 
console.log(stringToSlug2(title2));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Hier habe ich die oben erwähnte Funktion mit zwei verschiedenen Sprachen implementiert. Funktion stringToSlug1 mit Englisch und stringToSlug2 mit nepalesischer Sprache. Mit englischem Text funktioniert die Funktion gut, aber wenn der Text in einer anderen Sprache ist, geben die Funktionen nur - zurück. Ergebnis I von Funktion stringToSlug2 erreichen wollen ist घर-घरमा-ग्यास-पाइप-कार्यान्वयनको-जिम्मा-ओलीकै-काँधमा

+2

Try '[\ p {L} \ p {Digit} _]' statt '\ W ',' \ W 'passt nur ASCII (leider). – Majora320

+0

Ich möchte Sonderzeichen ersetzen wie:,!, # @ $$ # @ ^% #^ –

+1

Ersetze '.replace (/ [^ \ w \ -] +/g, '')' mit '.replace (/ [^ \ p {L} \ p {Ziffer} _ \ -] +/g, '') ' – Majora320

Antwort

1

Based . https://stackoverflow.com/a/18936783/5740382 auf eine Antwort

ich habe mit einer Lösung zu kommen, wenn es nicht gute Lösung (I guess) ist ich werde einige Angebote Charakter mit .replace(/([[email protected]#$%^&*()_+= filtern {} [] \ |. \ :;‘<> ,. /?]) +/g, '-') regex instead of filtering all non-word chars with .replace (/ [^ \ w -] +/g, '') `. Also hier ist meine jQuery-Funktion.

var title = 'घर-घरमा ग्यास पाइपः कार्यान्वयनको जिम्मा ओलीकै काँधमा !'; 
 

 
function stringToSlug (title) { 
 
    return title.toLowerCase().trim() 
 
    .replace(/\s+/g, '-')   // Replace spaces with - 
 
    .replace(/&/g, '-and-')   // Replace & with 'and' 
 
    .replace(/([[email protected]#$%^&*()_+=`{}\[\]\|\\:;'<>,.\/? ])+/g, '-') // Replace sepcial character with - 
 
    .replace(/\-\-+/g, '-')   // Replace multiple - with single - 
 
} 
 
console.log(stringToSlug(title));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

1

Leider sind die Designer von regulären Ausdrücken (die, die in JavaScript, sowieso) denken nicht viel über Internationalisierung bei der Gestaltung. \w nur Übereinstimmungen a-z, A-Z und _, und so [^\w\-]+ bedeutet [^a-zA-Z_\-]+. Andere Dialekte von regulären Ausdrücken haben ein Unicode-fähiges Wortmuster, aber die beste Wahl für JavaScript ist eine schwarze Liste von Symbolen zu haben (Sie :!#@$$#@^%#^ erwähnt. Sie, dass wie [:!#@$$#@^%#^]+ mit etwas tun kann (statt [^\w\-]+).

Verwandte Themen