JavaScript RegExp - ^.{2}$



Description

^.{2}$ matches any string containing exactly two characters.

Example

Following example shows usage of RegExp expression.

<html> <head> <title>JavaScript RegExp</title> </head> <body> <script type = "text/javascript"> var str = "pa"; var pattern = /^.{2}$/g; var result = str.match(pattern); document.write("Test 1 - returned value : " + result); str = "pap"; result = str.match(pattern); document.write("<br/>Test 2 - returned value : " + result); </script> </body> </html>

Output

Test 1 - returned value : pa
Test 2 - returned value : null
Advertisements