1
我有一個正則表達式是搜索網頁的源它通過Ajax檢索並獲取所有數據和插圖中包括字段集標籤。Javascript正則表達式返回null,但它不應該是。爲什麼?
這裏是我的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正則表達式的作品。
但是,當所有的說法和完成後,我得到空返回。
我在做什麼錯?
謝謝!
請注意,這是在測試的形式,它可能看起來很醜,但是我試着一步打破它一步,這是最後的狀態是英寸
謝謝Guffa。我不得不逃脫另一個,這工作: var regex = new RegExp(「<(fieldset)\\ b [^>] *>。*?\\1>」); 有了這樣一個簡單的,直截了當的答案,我應該刪除這個問題嗎? – 2009-04-21 23:30:17