Javascript regex returning null but it shouldn’t be. Why


I have a regex that searches the source of a page it retrieves via Ajax and gets all the data inbetween and including the fieldset tags.

Here's my javascript:

 var req = new XMLHttpRequest();
 var regex = new RegExp("<(fieldset)\b[^>]*>.*?</\1>");

 function showEditForm(i) {'GET', '/admin/maps/edit/' + i, false);

   var response = req.responseText;
   //strip out all the line breaks
   var responseStripped = response.replace( new RegExp("\\n|\\r", "g"), "" );

   var regexed = regex.exec(responseStripped)


Here's the source of responseStripped:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       ""><html xmlns="" xml:lang="en" lang="en"><head>  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />  <title>Gessner Engineering | Administrative Area | edit</title>  <link href="/stylesheets/scaffold.css?1236625014" media="screen" rel="stylesheet" type="text/css" />  </head><body>  <div id="user-bar-greeting">Logged in as Thomas Gessner</div>  <div id="user-bar-action"  >(<a href="/logout" title="Log out">Log out</a>)</div><div id="header"></div><ul id="menu">  <li><a href="/admin">Home</a></li>  <li><a href="/admin/maps">Geotechnical Map</a></li>  <li><a href="/admin/profiles">Profiles</a></li>  <li><a href="/admin/projects">Projects</a></li>  <li><a href="/admin/users">Users</a></li></div><div id="content">  <h1></h1>  <fieldset><legend>Edit Test Marker</legend><form action="/admin/maps/1" class="edit_map" enctype="multipart/form-data" id="edit_map_1" method="post"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="1422cd87ff4d68b2b23c9015a0568bffd47fbfeb" /></div>    <p>    <label for="map_name">Name</label><br />    <input id="map_name" name="map[name]" size="30" type="text" value="Test Marker" />  </p>  <p>    <label for="map_description">Description</label><br />    <textarea cols="40" id="map_description" name="map[description]" rows="5">test</textarea>  </p>  <p>    <label for="category_id">Category:</label><br/>    <select id="map_category_id" name="map[category_id]"><option value="">Select One</option></select>  </p>  <p>    <label for="map_address">Address</label><br />    <input id="map_address" name="map[address]" size="30" type="text" />  </p>  <p>    <label for="map_address2">Address2</label><br />    <input id="map_address2" name="map[address2]" size="30" type="text" />  </p>  <p>    <label for="map_city">City</label><br />    <input id="map_city" name="map[city]" size="30" type="text" />  </p>  <p>    <label for="map_state">State</label><br />    <input id="map_state" name="map[state]" size="2" type="text" />  </p>  <p>    <label for="map_zip">Zip</label><br />    <input id="map_zip" name="map[zip]" size="30" type="text" />  </p>  <p>    <input id="map_submit" name="commit" type="submit" value="Create" />  </p></form></fieldset></div></body></html>

I've verified that the regex works in RegexBuddy and at RegexPal.

However, when all is said and done, I get null returned.

What am I doing wrong?


Please note this is in testing form, it may look ugly but I tried to break it down step by step and this is the last state it was in.

Best Solution

You have forgotten to escape the backslash in the string:

var regex = new RegExp("<(fieldset)\\b[^>]*>.*?</\1>");