我有一个正则表达式,它通过Ajax搜索它检索的页面的源,并获取其间的所有数据,包括fieldset标记。
这是我的javascript:
var req = new XMLHttpRequest();
var regex = new RegExp("<(fieldset)\b[^>]*>.*?</\1>");
function showEditForm(i) {
req.open('GET', '/admin/maps/edit/' + i, false);
req.send(null);
var response = req.responseText;
//strip out all the line breaks
var responseStripped = response.replace( new RegExp("\\n|\\r", "g"), "" );
var regexed = regex.exec(responseStripped)
alert(regexed);
}
这是responseStripped的来源:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" 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>
我已经确认正则表达式适用于RegexBuddy和RegexPal。
但是,当完成所有操作后,我将返回null。
我做错了什么?
谢谢!
请注意这是在测试形式,它可能看起来很丑,但我试图一步一步地分解它,这是它的最后状态。
答案 0 :(得分:7)
你忘了逃避字符串中的反斜杠:
var regex = new RegExp("<(fieldset)\\b[^>]*>.*?</\1>");