2013-02-26 76 views
1

我刚刚发现jquery trim函数(使用1.8.x)不会修剪日语空格。 这个问题有什么更好的解决办法?Jquery trim不工作日语空格

+1

考试请请吗? – mplungjan 2013-02-26 16:03:10

+1

http://stackoverflow.com/questions/4300980/what-are-all-the-japanese-whitespace-characters – mplungjan 2013-02-26 16:03:54

+0

因为你是关于编码的话题,请阅读这篇文章,当有人给出这篇文章时,我发现它很有帮助它对我来说:http://www.joelonsoftware.com/articles/Unicode.html – Kristian 2013-02-26 16:05:45

回答

1

我写的东西看完这个:

https://github.com/jquery/jquery/pull/896

DEMO

$.extend({ 
    jTrim: function(str){ 
    var re = /^[\s\xA0\uFEFF\u1680\u180E\u2000-\u200A\u202F\u205F\u3000]+|[\s\xA0\uFEFF\u1680\u180E\u2000-\u200A\u202F\u205F\u3000]+$/g 
    return str.replace(re,""); 
    } 
}); 

console.log(">"+$.jTrim(' よろしくお願い申し上げます。')+"<"); 
+0

谢谢你。我发现问题实际上是基于浏览器的。 jquery修剪在IE9中工作正常,但我不得不支持IE> = 6 :( – Paulus 2013-02-27 23:42:15

1

对于ES5垫片包括(据说)完整地实现ES5 String.trim()方法,请参阅https://github.com/kriskowal/es5-shim

// ES5 15.5.4.20 
// http://es5.github.com/#x15.5.4.20 
var ws = "\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003" + 
    "\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028" + 
    "\u2029\uFEFF"; 
if (!String.prototype.trim || ws.trim()) { 
    // http://blog.stevenlevithan.com/archives/faster-trim-javascript 
    // http://perfectionkills.com/whitespace-deviations/ 
    ws = "[" + ws + "]"; 
    var trimBeginRegexp = new RegExp("^" + ws + ws + "*"), 
     trimEndRegexp = new RegExp(ws + ws + "*$"); 
    String.prototype.trim = function trim() { 
     if (this === undefined || this === null) { 
      throw new TypeError("can't convert "+this+" to object"); 
     } 
     return String(this) 
      .replace(trimBeginRegexp, "") 
      .replace(trimEndRegexp, ""); 
    }; 
} 
+0

谢谢,它工作正常。 – Paulus 2013-02-27 23:42:34