如何使用jQuery将unicode字符串转换为正确的字符?

时间:2017-12-28 05:17:07

标签: javascript jquery unicode

这里我有以下函数将字符串转换为slug以使SEO友好URL。

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>
&#13;
&#13;
&#13;

这里我用两种不同的语言实现了上述功能。功能stringToSlug1使用英语,stringToSlug2使用尼泊尔语。使用英文文本时,该功能正常工作,但当文本使用其他语言时,上述功能仅返回 - 。 我希望从功能stringToSlug2实现的结果是घर-घरमा-ग्यास-पाइप-कार्यान्वयनको-जिम्मा-ओलीकै-काँधमा

2 个答案:

答案 0 :(得分:1)

不幸的是,正则表达式的设计者(无论如何都是JavaScript)在设计时并没有考虑国际化。 \w仅匹配a-zA-Z_,因此[^\w\-]+表示[^a-zA-Z_\-]+。正则表达式的其他方言具有启用unicode的单词模式,但您最好的选择是使用符号黑名单(您提到:!#@$$#@^%#^。您可以使用[:!#@$$#@^%#^]+(而不是[^\w\-]+ <html> <head> <title>check</title> <script type='text/javascript'> function checkRun(){ var btn = document.querySelector('button'); btn.onclick = function() { btn.style.backgroundColor='red'; } } </script> </head> <body> <input type='text' name='checkName' id='check1' value='val1' class='class1'/><br><br> <button type='button' name='checkName' id='check3' value='val3' class='class3'>Hello</button> </body> </html> )。

答案 1 :(得分:0)

基于答案https://stackoverflow.com/a/18936783/5740382

我想出了一个解决方案,虽然这不是一个好的解决方案(我猜)。我会使用.replace(/([~!@#$%^&*()_+= {} [] \ | \:;&#39;&lt;&gt;,。/?来过滤一些特殊字符。 ])+ / g,&#39; - &#39;)regex instead of filtering all non-word chars with。replace(/ [^ \ w - ] + / g,&#39;&#39;)`。所以这是我的jQuery函数。

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

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