Regular expressions RegExp JavaScript in Google Analytics

Regular expressions RegExp JavaScript in Google Analytics

Regular expressions (regexp or regex) are used not only by JavaScript developers but also by web analysts for Google Analytics.

  • . – any single character;
  • \ – escape special character meanings (used to escape +, -, ., etc.);
  • [...] – match any character listed inside the square brackets;
  • - – defines a range (for example: [1-9]);
  • ^ – negates characters that follow it (only works inside square brackets, for example: [^1-9]);
  • ^ – asserts the start of the string (must be at the beginning);
  • $ – asserts the end of the string (must be at the end);
  • ? – matches zero or one of the preceding element (for example: 21? matches "2" or "21");
  • + – matches one or more of the preceding element (for example: 21+ matches "21", "211", "2111", etc.);
  • * – matches zero or more of the preceding element (for example: 21* matches "2", "21", "211", "2111", etc.);
  • {min,max} – quantifier specifying minimum and maximum repetitions (for example: 21{2} matches "211" only; 21{1,3} matches "21", "211", "2111" but not "2" or "21111");
  • .* – any sequence of characters of any length;
  • () – groups and captures a subpattern as a group;
  • | – OR operator (for example: 1|2 means "1" OR "2");
  • \d – any digit (same as [0-9]);
  • \s – any whitespace character (space, tab, newline);
  • \w – any word character: letter, digit, or underscore (same as [A-Za-z0-9_]).