Why does this javascript split function replace \1?

I cannot understand the function used below that just splits a string on ‘.’.

Could you help me understand why it uses the extra replace statements?

function dotSplit (str) {
  return str.replace(/\1/g, '\u0002LITERAL\\1LITERAL\u0002')
    .replace(/\\\./g, '\u0001')
    .split(/\./).map(function (part) {
      return part.replace(/\1/g, '\\.')
      .replace(/\2LITERAL\\1LITERAL\2/g, '\u0001')
    })
}

48 thoughts on “Why does this javascript split function replace \1?”