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:
true
>
/a+/.test('a');Result:
true
>
/a+/.test('');Result:
false
>
/cat/.test('caaat');Result:
false
>
/ca+t/.test('caaat');Result:
true
>
/ca+t/.test('ct');Result:
false
+works with., just like it does with literal characters.>
/.+/.test('a');Result:
true
>
/.+/.test('cat');Result:
true
>
/.+/.test('');Result:
false
>
/a.+z/.test('aveloz');Result:
true
>
/a.+z/.test('az');Result:
false
The
*operator is similar to+, but means "zero or more times".>
/a*/.test('a');Result:
true
>
/a*/.test('aa');Result:
true
>
/a*/.test('');Result:
true
>
/a.*z/.test('aveloz');Result:
true
>
/a.*z/.test('az');Result:
true
We can use multiple
+and*s in the same regex.>
/a+b*c+/.test('aacc');Result:
true
>
/a+b*c+/.test('aa');Result:
false
>
/a+b*c+/.test('aabccc');Result:
true
>
/a+b*c+/.test('abbbbc');Result:
true
>
/a+b*c+/.test('bc');Result:
false