2017-12-28 33 views
2

여기에 문자열을 슬러그로 변환하여 다음과 같은 SEO 친숙한 URL을 만드는 기능이 있습니다. jQuery를 사용하여 유니 코드 문자열을 적절한 문자로 변환하는 방법은 무엇입니까?

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>
나는 두 개의 서로 다른 언어와 위에서 언급 한 기능을 구현 한 다음

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 - 
    } 
. 기능이 영어로 stringToSlug1이고 네팔 언어로 stringToSlug2입니다. 영어 텍스트의 경우 함수는 정상적으로 작동하지만 텍스트가 위에 언급 된 다른 언어의 함수 인 경우에만 반환됩니다. 나는 기능 stringToSlug2에서 달성하고자하는 결과 घर-घरमा-ग्यास-पाइप-कार्यान्वयनको-जिम्मा-ओलीकै-काँधमा입니다

+2

시도 '[\ P {L} \ {피 디지트} _]는 w''\ '\ w' 단을 ASCII (불행히도) 일치한다. – Majora320

+0

다음과 같은 특수 문자를 대체하고 싶습니다 :!, # @ $$ # @^% #^ –

+1

'.replace (/ [^ \ w \ -] +/g, '')'를'.replace [ – Majora320

답변

1

기반을 .. 그것이 좋은 해결책은 (내 생각) 내가 .replace(/([[email protected]#$%^&*()_+= 일부 스페셜 문자를 필터링 할 수 없습니다 불구하고 대답 https://stackoverflow.com/a/18936783/5740382

에 내가 해결책을왔다 {} [] \ | \ :; '<> ,. /?]) +/g, '-') regex instead of filtering all non-word chars with .replace (/ [^ \ w -] +/g, '')`. 그래서 여기 jQuery 함수가 있습니다. 대신`의

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

불행하게도, 정규 표현식의 디자이너 (자바 스크립트 사람, 어쨌든이) 많이 생각하지 않았다 국제화를 디자인 할 때. \wa-z, A-Z_과 일치하므로 [^\w\-]+[^a-zA-Z_\-]+을 의미합니다. 정규 표현식의 다른 방언은 유니 코드 사용 가능 워드 패턴을 가지고 있지만, 자바 스크립트에 대한 최선의 방법은 기호를 (당신이 :!#@$$#@^%#^을 언급의 블랙리스트를 가지고있다. 당신은 [:!#@$$#@^%#^]+ (대신 [^\w\-]+)와 같은 뭔가 그렇게 할 수 있습니다.