Javascript RegEx replacing "
I'm trying to patch the Drupal Panels module to fix this nasty issue, which makes the AJAX errorhandler completely unreadable. The error handler has this:
// Replace all < and > by < and >
error_text = error_text.replace("/&(lt|gt);/g", function (m, p) {
return (p == "lt")? "<" : ">";
});
I've tried two approaches:
Adding
// Now replace all " with ' for readability, for goodness sakes, per
// http://drupal.org/node/1124042
error_text = error_text.replace(/"/g, "'");
Modifying
// Replace all < and > by < and >
error_text = error_text.replace("/&(lt|gt|quot);/g", function (m, p) {
return (p == "lt")? "<" : (p == "gt") ? ">" : "'";
});
Yet neither has worked. Little help?
EDIT: When printed out to console (in FF4 or Chrome) The HTML entities are not shown. The resulting alert(""), however, looks like so:
An error occurred at /home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center.
Error Description: [ { "command": "settings", "argument": { "basePath": "/home/", "admin_menu": { "margin_top": 1 }, "asu_webauth": { "callapp":
>"home", quot;onLogoutURL": "http://physics-dev.asu.edu" } } }, { "command": "css_files", "argument": [ { "file": "/home/modules/node/node.css?E", "media": "all" }, { "file": "/home/modules/system/defaults.css?E", "media": "all" }, { "file": "/home/modules/system/system.css?E", "media": "all" }, { "file": "/home/modules/system/system-menus.css?E", "media": "all" }, { "file": "/home/modules/user/user.css?E", "media": "all" }, { "file": "/home/sites/all/modules/admin_menu/admin_menu.css?E", "media": "all" }, { "file": "/home/sites/all/modules/cck/theme/content-module.css?E", "media": "all" }, { "file": "/home/sites/all/modules/ctools/css/ctools.css?E", "media": "all" }, { "file": "/home/sites/all/modules/date/date.css?E", "media": "all" }, { "file": "/home/sites/all/modules/filefield/filefield.css?E", "media": "all" }, { "file": "/home/sites/all/modules/office_hours/office_hours.css?E", "media": "all" }, { "file": "/home/sites/all/modules/og/theme/og.css?E", "media": "all" }, { "file": "/home/sites/all/modules/panels/css/panels.css?E", "media": "all" }, { "file": "/home/sites/all/modules/views_slideshow/contrib/views_slideshow_singleframe/views_slideshow.css?E", "media": "all" }, { "file": "/home/sites/all/modules/views_slideshow/contrib/views_slideshow_thumbnailhover/views_slideshow.css?E", "media": "all" }, { "file": "/home/misc/farbtastic/farbtastic.css?E", "media": "all" }, { "file": "/home/sites/all/modules/calendar/calendar.css?E", "media": "all" }, { "file": "/home/sites/all/modules/cck/modules/fieldgroup/fieldgroup.css?E", "media": "all" }, { "file": "/home/sites/all/modules/views/css/views.css?E", "media": "all" } ] }, { "command": "scripts", "argument": [ "/home/misc/jquery.js", "/home/misc/drupal.js", "/home/sites/all/modules/admin_menu/admin_menu.js", "/home/sites/all/modules/og/og.js", "/home/sites/all/modules/panels/js/panels.js", "/home/sites/all/modules/views_slideshow/js/jquery.cycle.all.min.js", "/home/sites/all/modules/views_slideshow/contrib/views_slideshow_singleframe/views_slideshow.js", "/home/sites/all/modules/views_slideshow/contrib/views_slideshow_thumbnailhover/views_slideshow.js", "/home/sites/all/modules/asu_webauth/asu_webauth.js" ] }, { "command": "modal_display", "title": "Add content to Center", "output": "\x3cdiv class=\"panels-add-content-modal\"\x3e\x3cdiv class=\"panels-section-column panels-section-column-categories\"\x3e\x3cdiv class=\"inside\"\x3e\x3cdiv class=\"panels-categories-box\"\x3e\x3ca href=\"/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/activity\" class=\"ctools-use-ajax panels-modal-add-category\" title=\"\"\x3eActivity\x3c/a\x3e\x3ca href=\"/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/menus\" class=\"ctools-use-ajax panels-modal-add-category\" title=\"\"\x3eMenus\x3c/a\x3e\x3ca href=\"/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/miscellaneous\" class=\"ctools-use-ajax panels-modal-add-category\" title=\"\"\x3eMiscellaneous\x3c/a\x3e\x3ca href=\"/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/page-elements\" class=\"ctools-use-ajax panels-modal-开发者_运维知识库add-category\" title=\"\"\x3ePage elements\x3c/a\x3e\x3ca href=\"/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/view-panes\" class=\"ctools-use-ajax panels-modal-add-category\" title=\"\"\x3eView panes\x3c/a\x3e\x3ca href=\"/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/views\" class=\"ctools-use-ajax panels-modal-add-category\" title=\"\"\x3eViews\x3c/a\x3e\x3ca href=\"/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/widgets\" class=\"ctools-use-ajax panels-modal-add-category\" title=\"\"\x3eWidgets\x3c/a\x3e\x3c/div\x3e\x3cdiv class=\"content-type-button clear-block\"\x3e\x3ca href=\"/home/panels/ajax/editor/add-pane/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/node/node\" class=\"ctools-use-ajax panels-modal-add-config\" title=\"Add a node from your site as content.\"\x3e\x3cimg src=\"/home/sites/all/modules/ctools/plugins/content_types/node/icon_node.png\" alt=\"\" title=\"\" width=\"16\" height=\"16\" /\x3e\x3c/a\x3e\x3cdiv\x3e\x3ca href=\"/home/panels/ajax/editor/add-pane/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/node/node\" class=\"ctools-use-ajax panels-modal-add-config\" title=\"Add a node from your site as content.\"\x3eExisting node\x3c/a\x3e\x3c/div\x3e\x3c/div\x3e\x3cdiv class=\"content-type-button clear-block\"\x3e\x3ca href=\"/home/panels/ajax/editor/add-pane/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/custom/custom\" class=\"ctools-use-ajax panels-modal-add-config\" title=\"Create a completely custom piece of HTML content.\"\x3e\x3cimg src=\"/home/sites/all/modules/ctools/images/no-icon.png\" alt=\"\" title=\"\" width=\"16\" height=\"16\" /\x3e\x3c/a\x3e\x3cdiv\x3e\x3ca href=\"/home/panels/ajax/editor/add-pane/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/custom/custom\" class=\"ctools-use-ajax panels-modal-add-config\" title=\"Create a completely custom piece of HTML content.\"\x3eNew custom content\x3c/a\x3e\x3c/div\x3e\x3c/div\x3e\x3c/div\x3e\x3c/div\x3e\x3cdiv class=\"panels-categories-description\"\x3eContent options are divided by category. Please select a category from the left to proceed.\x3c/div\x3e\x3c/div\x3e" } ]
str.replace(/&(l|g|quo)t;/g, function(a,b){
return {
l : '<',
g : '>',
quo : '"'
}[b];
})
remove the quotes around the regex
error_text = error_text.replace(/&(lt|gt|quot);/g, function (m, p) {
return (p == "lt")? "<" : (p == "gt") ? ">" : "'";
});
demo http://jsfiddle.net/gaby/mfmkc/
This should work:
var str = '<span>"error"</span>';
str.replace(/&(lt|gt|quot);/g, function(m, t) {
switch (t) {
case 'lt':
return '<';
case 'gt':
return '>';
case 'quot':
return '\'';
}
});
It returns "<span>'error'</span>"
If that's not working, then perhaps your original string contains &lt;
instead of <
, etc...
精彩评论