I Can Kill Your Browser with a Simple Regex

Maciek Rząsa, @mjrzasa, Toptal

View My GitHub Profile

Regex Performance Exercises

Test Apps

Exercise 1

https://link.do/spa-greedy

Match HTML tags.

  1. Add text inside/after the tag, see if step count changes; see debugger
  2. Add another tag, see the result
  3. Two solutions: limit repetition using lazy quantifier .*?, or limit scope [^>]
  4. Try both, add text inside/after the tag, see step count changes

Hints:

Exercise 2

https://link.do/spa-possessive

Match numbers with units ending with semicolon: 123cm; 32kg; 1m3;

  1. Try to add digits to the number
  2. Remove semicolon - see steps and backtracking in debugger
  3. Replace greedy quantifier with the possessive one ++, see steps in debugger

Hints:

Exercise 3

Optimize those two expressions:

  1. Match Tea column in CSV text: https://link.do/spa-csv
  2. (*) Find some CSS classes related to product: product-size, product-column, product-info and product ids that has digits 1,2,3. https://link.do/spa-css

Hints:

Exercise 5 (*)

https://link.do/spa-operations

Arithmetic operations.

You have a regex matching simple arithmetic operations. Allowed: two numbers separated with plus or minus sign, ending with equals sign, e.g. 12+34= or 32121-23=

  1. Enhance regex to allow 3 numbers and 2 signs (e.g. 12+322-1= ).
  2. Enhance regex to allow any lenght of the operation (e.g. 12+322-1+223-2323+...=).
  3. Remove equals sign from the test string, check steps in debugger.

References