javascript

JavaScript functions for basename and dirname

Here are two JavaScript functions that mimic their PHP cousins.

basename()

function basename(path) {
  return path.replace(/.*\//, '');
}

dirname()

function dirname(path) {
  return path.match(/.*\//);
}

Unlike their PHP cousins, these functions do not support paths separated with backslashes.

esc