UnitTest( xregexp.asp )

Output

2012
["2","3","4"]
02/22/2012
02/22/2012
True
02/22/2012
02/22/2012
02/22/2012
2012
[2,4]
["2","4","56"]
["xregexp.com","www.google.com"]
/a\+b\*c|(dog)\1|(cat)\2/i
True
True
True
True
True

Sources

/axe-ut/xregexp.unit.test.asp

<!--#include virtual="/lib/unit-tests.asp"-->
<!--#include virtual="/lib/axe/classes/unittest.asp"-->
<!--#include virtual="/lib/axe/classes/Parsers/json2.asp"-->
<!--#include virtual="/lib/axe/classes/Utilities/xregexp.asp"-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:lang="en">
    <head>
        <title>AXE - Unit Test - XRegExp</title>
        <!--// meta tags //-->
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <!--// link tags //-->
        <link rel="stylesheet" type="text/css" media="screen" href="/lib/unit-tests.css" />
    </head>
    <body>
        <div id="container">
            <div id="container-hd">
                <h1>UnitTest( xregexp.asp )</h1>
            </div>
            <div id="container-bd">
 
<h2>Output</h2>
<div class="code"><pre><%
 
dim ReDate, Match, pos, result, arr, html, unicodeWord
 
' Using named capture and flag x (free-spacing and line comments)
set ReDate = XRegExp( join( array( _
    "(?<year>  [0-9]{4} ) -?  # year " _
  , "(?<month> [0-9]{2} ) -?  # month" _
  , "(?<day>   [0-9]{2} )     # day  " _
), vbLf ) , "x" )
 
' XRegExp.exec gives you named backreferences on the match result
set Match = XRegExp.exec("2012-02-22", ReDate)
Response.write( Match.year )' -> '2012'
Response.write( vbNewline )
 
' It also includes optional pos and sticky arguments
pos = 3
set result = JSON.parse("[]")
while( not isNull( XRegExp.exec( S("<1><2><3><4>5<6>"), XRegExp("<(\d+)>"), pos, "sticky" ) ) )
    set Match = XRegExp.exec( S("<1><2><3><4>5<6>"), XRegExp("<(\d+)>"), pos, "sticky" )
    result.push( Oi(Match, 1) )
    pos = Match.index + S( Oi(Match, 0) ).length
wend
Response.write( JSON.stringify( result ) )' result -> ['2', '3', '4']
Response.write( vbNewline )
 
' XRegExp.replace allows named backreferences in replacements
Response.write( XRegExp.replace( "2012-02-22", ReDate, "${month}/${day}/${year}" ) )' -> '02/22/2012'
Response.write( vbNewline )
 
Response.write( XRegExp.replace( "2012-02-22", ReDate, lambda( join( array( _
  "function(match) {" _
, "    return match.month + '/' + match.day + '/' + match.year;" _
, "}" _
) , vbNewline ) ) ) )' -> '02/22/2012'
Response.write( vbNewline )
 
' In fact, XRegExps compile to RegExps and work perfectly with native methods
Response.write( ReDate.test("2012-02-22") )' -> true
Response.write( vbNewline )
 
' The *only* caveat is that named captures must be referenced using numbered backreferences
Response.write( S("2012-02-22").replace(ReDate, "$2/$3/$1") )' -> '02/22/2012'
Response.write( vbNewline )
 
' If you want, you can extend native methods so you don't have to worry about this.
' Doing so also fixes numerous browser bugs in the native methods
XRegExp.install("natives")
Response.write( S("2012-02-22").replace(ReDate, "${month}/${day}/${year}") )' -> '02/22/2012'
Response.write( vbNewline )
 
Response.write( S("2012-02-22").replace(ReDate, lambda( join( array( _
  "function(match) {" _
, "    return match.month + '/' + match.day + '/' + match.year;" _
, "}" _
) , vbNewline ) ) ) )' -> '02/22/2012'
Response.write( vbNewline )
 
Response.write( ReDate.exec("2012-02-22").year )' -> '2012'
Response.write( vbNewline )
 
' Extract every other digit from a string using XRegExp.forEach
Response.write( JSON.stringify( _
    XRegExp.forEach( _
        "1a2345" _
      , XRegExp("\d") _
      , lambda("function(match, i){ if(i % 2) this.push(+match[0]) ; }") _
      , JSON.parse("[]") _
    ) _
) )' -> [2, 4]
Response.write( vbNewline )
 
' Get numbers within <b> tags using XRegExp.matchChain
set arr = JSON.parse("[]")
arr.push( XRegExp("(?is)<b>.*?</b>") )
arr.push( XRegExp("\d+") )
Response.write( JSON.stringify( _
    XRegExp.matchChain( "1 <b>2</b> 3 <b>4 a 56</b>", arr ) _
) )' -> ['2', '4', '56']
Response.write( vbNewline )
 
' You can also pass forward and return specific backreferences
html = "<a href='http://xregexp.com/'>XRegExp</a>" _
     & "<a href='http://www.google.com/'>Google</a>"
 
set arr = js_eval( join( array( _
  "[" _
, "{regex: /<a href='([^']+)'>/i, backref: 1}," _
, "{regex: XRegExp('(?i)^https?://(?<domain>[^/?#]+)'), backref: 'domain'}" _
, "]" _
), vbNewline ) )
 
Response.write( JSON.stringify( _
    XRegExp.matchChain( html, arr ) _
) )' -> ['xregexp.com', 'www.google.com']
Response.write( vbNewline )
 
' Merge strings and regexes into a single pattern, safely rewriting backreferences
set arr = JSON.parse("[]")
arr.push("a+b*c")
arr.push( XRegExp("(dog)\1") )
arr.push( XRegExp("(cat)\1") )
 
Response.write( XRegExp.union(arr, "i") )' -> /a\+b\*c|(dog)\1|(cat)\2/i
Response.write( vbNewline )
 
' Unicode support
set unicodeWord = XRegExp("^\p{L}+$")
Response.write( unicodeWord.test("Русский") )' -> true
Response.write( vbNewline )
Response.write( unicodeWord.test("日本語") )' -> true
Response.write( vbNewline )
Response.write( unicodeWord.test("العربية") )' -> true
Response.write( vbNewline )
 
Response.write( XRegExp("^\p{Hiragana}+$").test("ひらがな") )' -> true
Response.write( vbNewline )
Response.write( XRegExp("^[\p{Latin}\p{Common}]+$").test("Über Café.") )' -> true
Response.write( vbNewline )
 
%></pre></div>
 
            </div>
            <div id="container-ft">
                <h2>Sources</h2>
                <h3><%= Request.ServerVariables("SCRIPT_NAME") %></h3>
                <div class="code"><%= geshify( loadTextFile( Server.mapPath( Request.ServerVariables("SCRIPT_NAME") ) ), "asp" ) %></div>
            </div>
        </div>
        <!--// javascript tags //-->
    </body>
</html>
<script language="Javascript" runat="server">
// some helpers
function S(s) { return new String(s); }
function Oi(O, i) { return O[i]; }
function js_eval(c) { return eval(c); }
</script>