JavaScript Regular Expression Testing

March 12, 2010

Here's the source for a simple web page, using jQuery, which allows to user to do some simple experimentation with regular expressions in JavaScript.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<title>RegEx Test</title>
<script src="" type="text/javascript"></script>
li { list-style-type: none; }
<h2>RegEx Test</h2>
        <td>Regular Expression:</td>
        <td><input type="text" id="regex" size="80"/></td>
        <td><input type="text" id="options" size="10" value="gm"/></td>
  <li>Text to search:</li>
  <li><textarea rows="6" cols="80" id="sourcetext"></textarea></li>
  <li><input type="submit" id="testbutton" value="Run Expression"></li>
<div id="resultstext"></div>

<script type="text/javascript">
    var lRegExText = '(' + $('#regex').val() + ')';
    var lRegExOptions = $('#options').val();
    var lText = $('#sourcetext').val();
    var lRegEx = new RegExp(lRegExText, lRegExOptions);
    var lResults = lText.replace(lRegEx, "<font color='red'><b>$1</b></font>");