regex vs indexof

Edit on GitHub

Setup

Tests

  • regex

    var text = "This is some text [meta&:blocks] repeats [repeated:2]."
    var regex = /\[.{1,}?\]/g;
    var matches = text.match(regex);
  • indexof

    var text = "This is some text [meta&:blocks] repeats [repeated:2]."
    var startIdx = text.indexOf('[');
    var meta = new Array();
    var clean = "";
    while (startIdx !== -1) {
      var endIdx = text.indexOf(']') + 1;
      meta.push(text.split("").splice(startIdx, endIdx - startId).join(""));
      startIdx = text.indexOf('[', endIdx);
    }