ViewerJS str.replace如何使它工作

时间:2017-04-24 04:30:58

标签: javascript html

我正在看一下ViewerJS,看起来很合适我发现的唯一问题是标题是%20而不是空格。我知道为什么,但我无法弄清楚如何解决它。

所以看起来这个代码首先得到了解决方案。我如何在这里修复它以使其显示没有%20。

function getPDFFileNameFromURL(url) {
var reURI = /^(?:([^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
//            SCHEME      HOST         1.PATH  2.QUERY   3.REF
// Pattern to get last matching NAME.pdf
var reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
var splitURI = reURI.exec(url);
var suggestedFilename = reFilename.exec(splitURI[1]) ||
                       reFilename.exec(splitURI[2]) ||
                       reFilename.exec(splitURI[3]);
if (suggestedFilename) {
suggestedFilename = suggestedFilename[0];
if (suggestedFilename.indexOf('%') !== -1) {
  // URL-encoded %2Fpath%2Fto%2Ffile.pdf should be file.pdf
  try {
    suggestedFilename =
      reFilename.exec(decodeURIComponent(suggestedFilename))[0];
  } catch(e) { // Possible (extremely rare) errors:
    // URIError "Malformed URI", e.g. for "%AA.pdf"
    // TypeError "null has no properties", e.g. for "%2F.pdf"
  }
}
}
return suggestedFilename || 'document.pdf';
}

1 个答案:

答案 0 :(得分:0)

var x = 'Some%20string%20with%20spaces';
x = x.replace(/%20/g, ' ');
console.log(x);