여기에 문자열을 슬러그로 변환하여 다음과 같은 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에서 달성하고자하는
결과 घर-घरमा-ग्यास-पाइप-कार्यान्वयनको-जिम्मा-ओलीकै-काँधमा입니다
시도 '[\ P {L} \ {피 디지트} _]는 w''\ '\ w' 단을 ASCII (불행히도) 일치한다. – Majora320
다음과 같은 특수 문자를 대체하고 싶습니다 :!, # @ $$ # @^% #^ –
'.replace (/ [^ \ w \ -] +/g, '')'를'.replace [ – Majora320