2014-01-13 42 views

回答

1

基於此代碼: (https://github.com/apache/jackrabbit/blob/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java

和這個單元測試 (https://github.com/apache/jackrabbit/blob/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java

這也應該這樣做:

function escapeIllegalChars(name) { 
    var illegalChars = "%/:[]*|\t\r\n"; 
    var buffer = ""; 
     for (var i = 0; i < name.length; i++) { 
      var ch = name.charAt(i); 
      if (illegalChars.indexOf(ch) != -1 || 
       (ch == '.' && name.length < 3)|| 
       (ch == ' ' && (i === 0 || i == name.length - 1))) { 
       buffer += escape(ch);     
      } else { 
       buffer += ch; 
      } 
     } 
     return buffer; 
} 

你可以看到代碼和相關測試傳遞給JSBin: (http://jsbin.com/eciYodo/2/edit?js,output