Javascript RegEx 替换 "

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5957546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:56:45  来源:igfitidea点击:

Javascript RegEx replacing "

javascriptregex

提问by Visionary Software Solutions

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:

我正在尝试修补 Drupal Panels 模块以解决这个令人讨厌的问题,这使得 AJAX 错误处理程序完全不可读。错误处理程序有这个:

// Replace all &lt; and &gt; 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 &quot with ' for readability, for goodness sakes, per
  // http://drupal.org/node/1124042
  error_text = error_text.replace(/&quot;/g, "'");

Modifying

修改

// Replace all &lt; and &gt; 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:

编辑:当打印到控制台(在 FF4 或 Chrome 中)时,不显示 HTML 实体。然而,结果 alert("") 看起来像这样:

An error occurred at /home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center.

Error Description: [ { &quot;command&quot;: &quot;settings&quot;, &quot;argument&quot;: {    &quot;basePath&quot;: &quot;/home/&quot;, &quot;admin_menu&quot;: { &quot;margin_top&quot;: 1 }, &quot;asu_webauth&quot;: { &quot;callapp&quot;:
>&quot;home&quot;, quot;onLogoutURL&quot;: &quot;http://physics-dev.asu.edu&quot; } } }, { &quot;command&quot;: &quot;css_files&quot;, &quot;argument&quot;: [ { &quot;file&quot;: &quot;/home/modules/node/node.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/modules/system/defaults.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/modules/system/system.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/modules/system/system-menus.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/modules/user/user.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/admin_menu/admin_menu.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/cck/theme/content-module.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/ctools/css/ctools.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/date/date.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/filefield/filefield.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/office_hours/office_hours.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/og/theme/og.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/panels/css/panels.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/views_slideshow/contrib/views_slideshow_singleframe/views_slideshow.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/views_slideshow/contrib/views_slideshow_thumbnailhover/views_slideshow.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/misc/farbtastic/farbtastic.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/calendar/calendar.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/cck/modules/fieldgroup/fieldgroup.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/views/css/views.css?E&quot;, &quot;media&quot;: &quot;all&quot; } ] }, { &quot;command&quot;: &quot;scripts&quot;, &quot;argument&quot;: [ &quot;/home/misc/jquery.js&quot;, &quot;/home/misc/drupal.js&quot;, &quot;/home/sites/all/modules/admin_menu/admin_menu.js&quot;, &quot;/home/sites/all/modules/og/og.js&quot;, &quot;/home/sites/all/modules/panels/js/panels.js&quot;, &quot;/home/sites/all/modules/views_slideshow/js/jquery.cycle.all.min.js&quot;, &quot;/home/sites/all/modules/views_slideshow/contrib/views_slideshow_singleframe/views_slideshow.js&quot;, &quot;/home/sites/all/modules/views_slideshow/contrib/views_slideshow_thumbnailhover/views_slideshow.js&quot;, &quot;/home/sites/all/modules/asu_webauth/asu_webauth.js&quot; ] }, { &quot;command&quot;: &quot;modal_display&quot;, &quot;title&quot;: &quot;Add content to Center&quot;, &quot;output&quot;: &quot;\x3cdiv class=\&quot;panels-add-content-modal\&quot;\x3e\x3cdiv class=\&quot;panels-section-column panels-section-column-categories\&quot;\x3e\x3cdiv class=\&quot;inside\&quot;\x3e\x3cdiv class=\&quot;panels-categories-box\&quot;\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/activity\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3eActivity\x3c/a\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/menus\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3eMenus\x3c/a\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/miscellaneous\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3eMiscellaneous\x3c/a\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/page-elements\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3ePage elements\x3c/a\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/view-panes\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3eView panes\x3c/a\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/views\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3eViews\x3c/a\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/widgets\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3eWidgets\x3c/a\x3e\x3c/div\x3e\x3cdiv class=\&quot;content-type-button clear-block\&quot;\x3e\x3ca href=\&quot;/home/panels/ajax/editor/add-pane/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/node/node\&quot; class=\&quot;ctools-use-ajax panels-modal-add-config\&quot; title=\&quot;Add a node from your site as content.\&quot;\x3e\x3cimg src=\&quot;/home/sites/all/modules/ctools/plugins/content_types/node/icon_node.png\&quot; alt=\&quot;\&quot; title=\&quot;\&quot; width=\&quot;16\&quot; height=\&quot;16\&quot; /\x3e\x3c/a\x3e\x3cdiv\x3e\x3ca href=\&quot;/home/panels/ajax/editor/add-pane/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/node/node\&quot; class=\&quot;ctools-use-ajax panels-modal-add-config\&quot; title=\&quot;Add a node from your site as content.\&quot;\x3eExisting node\x3c/a\x3e\x3c/div\x3e\x3c/div\x3e\x3cdiv class=\&quot;content-type-button clear-block\&quot;\x3e\x3ca href=\&quot;/home/panels/ajax/editor/add-pane/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/custom/custom\&quot; class=\&quot;ctools-use-ajax panels-modal-add-config\&quot; title=\&quot;Create a completely custom piece of HTML content.\&quot;\x3e\x3cimg src=\&quot;/home/sites/all/modules/ctools/images/no-icon.png\&quot; alt=\&quot;\&quot; title=\&quot;\&quot; width=\&quot;16\&quot; height=\&quot;16\&quot; /\x3e\x3c/a\x3e\x3cdiv\x3e\x3ca href=\&quot;/home/panels/ajax/editor/add-pane/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/custom/custom\&quot; class=\&quot;ctools-use-ajax panels-modal-add-config\&quot; title=\&quot;Create a completely custom piece of HTML content.\&quot;\x3eNew custom content\x3c/a\x3e\x3c/div\x3e\x3c/div\x3e\x3c/div\x3e\x3c/div\x3e\x3cdiv class=\&quot;panels-categories-description\&quot;\x3eContent options are divided by category. Please select a category from the left to proceed.\x3c/div\x3e\x3c/div\x3e&quot; } ]

回答by Mark Kahn

str.replace(/&(l|g|quo)t;/g, function(a,b){
    return {
        l   : '<',
        g   : '>',
        quo : '"'
    }[b];
})

回答by Gabriele Petrioli

remove the quotes around the regex

删除正则表达式周围的引号

  error_text = error_text.replace(/&(lt|gt|quot);/g, function (m, p) { 
    return (p == "lt")? "<" : (p == "gt") ? ">" : "'";
  });

demohttp://jsfiddle.net/gaby/mfmkc/

演示http://jsfiddle.net/gaby/mfmkc/

回答by Matty F

This should work:

这应该有效:

var str = '&lt;span&gt;&quot;error&quot;&lt;/span&gt;';
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>"

它返回 "<span>'error'</span>"

If that's not working, then perhaps your original string contains &amp;lt;instead of &lt;, etc...

如果这不起作用,那么您的原始字符串可能包含&amp;lt;而不是&lt;等...