Execute Program

Regular Expressions: Repetition

Welcome to the Repetition lesson!

This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!

  • The + operator requires something to occur one or more times.

  • >
    /a+/.test('aaa');
    Result:
    truePass Icon
  • >
    /a+/.test('a');
    Result:
    truePass Icon
  • >
    /a+/.test('');
    Result:
    falsePass Icon
  • >
    /cat/.test('caaat');
    Result:
    falsePass Icon
  • >
    /ca+t/.test('caaat');
    Result:
    truePass Icon
  • >
    /ca+t/.test('ct');
    Result:
    falsePass Icon
  • + works with ., just like it does with literal characters.

  • >
    /.+/.test('a');
    Result:
    truePass Icon
  • >
    /.+/.test('cat');
    Result:
    truePass Icon
  • >
    /.+/.test('');
    Result:
    falsePass Icon
  • >
    /a.+z/.test('aveloz');
    Result:
    truePass Icon
  • >
    /a.+z/.test('az');
    Result:
    falsePass Icon
  • The * operator is similar to +, but means "zero or more times".

  • >
    /a*/.test('a');
    Result:
    truePass Icon
  • >
    /a*/.test('aa');
    Result:
    truePass Icon
  • >
    /a*/.test('');
    Result:
    truePass Icon
  • >
    /a.*z/.test('aveloz');
    Result:
    truePass Icon
  • >
    /a.*z/.test('az');
    Result:
    truePass Icon
  • We can use multiple + and *s in the same regex.

  • >
    /a+b*c+/.test('aacc');
    Result:
    truePass Icon
  • >
    /a+b*c+/.test('aa');
    Result:
    falsePass Icon
  • >
    /a+b*c+/.test('aabccc');
    Result:
    truePass Icon
  • >
    /a+b*c+/.test('abbbbc');
    Result:
    truePass Icon
  • >
    /a+b*c+/.test('bc');
    Result:
    falsePass Icon