JavaScript RegExp - g



Description

g performs a global match that is, find all matches rather than stopping after the first match.

Example

Following example shows usage of RegExp expression.

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

Output

Test 1 - returned value : abc,abc,abc
Test 2 - returned value : abc
Advertisements