Skip to content

Instantly share code, notes, and snippets.

@xionglun
Last active December 21, 2015 00:48
Show Gist options
  • Save xionglun/6222506 to your computer and use it in GitHub Desktop.
Save xionglun/6222506 to your computer and use it in GitHub Desktop.
double slash has not been replaced.

replace double slash

At seajs/src/util-path.js, double slash has not been replaced!

  var DOT_RE = /\/\.\//g
  var DOUBLE_DOT_RE = /\/[^/]+\/\.\.\//
  
  function realpath(path){
    path = path.replace(DOT_RE, '/')
    
    while(path.match(DOUBLE_DOT_RE)) {
      path = path.replace(DOUBLE_DOT_RE, '/')
    }
    
    console.log(path)
  }
  
  var fakepath = 'http://test.com/a//./b/../c'
  realpath(fakepath)      // 'http://test.com/a//c'

This function realpath does not replace double slash. I test with nodejs.

So, i add two line:

  var DOT_RE = /\/\.\//g
  var DOUBLE_DOT_RE = /\/[^/]+\/\.\.\//
  // replace double slash
  var DOUBLE_SLASH = /[^:]\/\//g
  
  function realpath(path){
    path = path.replace(DOT_RE, '/')
    
    while(path.match(DOUBLE_DOT_RE)) {
      path = path.replace(DOUBLE_DOT_RE, '/')
    }
    
    path = path.replace(DOUBLE_SLASH, '$1/');
    
    console.log(path)
  }
  
  var fakepath = 'http://test.com/a//./b/../c'
  realpath(fakepath)      // 'http://test.com/a/c'

Is it correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment