清理HTML标签属性

4

我需要使用JavaScript浏览大量HTML,以调整属性引号语法为全部双引号。我不需要担心只有键的属性,例如“disabled”或“selected”。

这是我的当前测试用例:

var text = "<input class=daily_input type='hidden' size='1' value=3 disabled />";
var regex = /<([\w]+)([^>]+)([\w]+)=['"]?([^'\s|"\s|\s]*)['"]?([^>]+)>/gi;
text = text.replace( regex, "<$1$2$3=\"$4\"$5>" );

console.log(text); // logs <input class=daily_input type='hidden' size='1' value="3" disabled />

看起来仍然只调整了最后一个属性。我可以在TextMate中使用正则表达式查找/替换轻松测试匹配,下面的内容将匹配文本HTML标记中的每个属性:

/([\w]+)=['"]?([^'\s|"\s|\s]*)['"]?/gi

我该如何修改这个代码,以便捕捉和调整每个属性,而不仅仅是最后一个属性?我已经尝试了相当长的时间,但没有结果。

2个回答

2
text.replace(/='([^']*)'/g, '="$1"').replace(/=([^"'][^ >]*)/g, '="$1"')

或者(替换一个):
text.replace(/='([^']*)'|=([^"'][^ >]*)/g, '="$1"')

1
首先,感谢你!它有效。我的唯一问题是能否在一个 replace() 中完成所有操作。HTML 文件可能会非常大,效率至关重要。不过我会尝试调整。 - thechriskelley
@thechriskelley:在一个“替换”中添加了一个解决方案。 - thejh

1
我知道这是一个晚回答,但如果您总是可以使用sanitize-html。它是为node编写的,但是我相信您可以针对该库(或者您的代码)运行browserify

请注意,它使用lodash,因此如果您已经在使用它,则可能需要调整包。

这个例子比你要找的还多... 我使用这个库来清理输入代码,将其转换为Markdown格式存储在数据库中,然后通过marked重新生成。

// convert/html-to-filtered-markdown.js

'use strict';

var sanitize = require('sanitize-html') //https://www.npmjs.org/package/sanitize-html
    ,toMarkdown = require('to-markdown').toMarkdown
    ;

module.exports = function convertHtmlToFilteredMarkdown(input, options) {
  if (!input) return '';

  options = options || {};

  //basic cleanup, normalize line endings, normalize/reduce whitespace and extra line endings
  var response = (input || '').toString().trim()
    .replace(/(\r\n|\r|\n)/g, '\n') //normalize line endings
    .replace(/“/g, '"') //remove fancy quotes
    .replace(/”/g, '"') //remove fancy quotes
    .replace(/‘/g, '\'') //remove fancy quotes
    .replace(/’/g, '\'') //remove fancy quotes
    ;

  //sanitize html input
  response = sanitize(response, {
    //don't allow table elements
    allowedTags: [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol', 'nl', 'li', 'b', 'i', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div', 'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre' ],

    //make orderd lists
    transformTags: {
      'ol': 'ul'
    }
  }).replace(/\r\n|\r|\n/g,'\n') //normalize line endings;

  if (!options.tables) {
    response = response.replace(/[\s\n]*\<(\/?)(table|thead|tbody|tr|th|td)\>[\s\n]*/g, '\n\n') //replace divs/tables blocks as paragraphs
  }

  //cleanup input further
  response = response
    .replace(/[\s\n]*\<(\/?)(div|p)\>[\s\n]*/g, '\n\n') //divs and p's to simple multi-line expressions
    .replace(/\>#/g, '\n\n#') //cleanup #'s' after closing tag, ex: <a>...</a>\n\n# will be reduced via sanitizer
    .replace(/\\s+\</,'<') //remove space before a tag open
    .replace(/\>\s+\n?/,'>\n') //remove space after a tag close
    .replace(/\&?nbsp\;?/g,' ') //revert nbsp to space
    .replace(/\<\h[12]/g,'<h3').replace(/\<\/\h[12]/g,'</h3') //reduce h1/h2 to h3
    ;

  //convert response to markdown
  response = toMarkdown(response);

  //normalize line endings
  response = response
    .replace(/(?:^|\n)##?[\b\s]/g,'\n### ') //reduce h1 and h2 to h3
    .replace(/(\r\n|\r|\n)/g, '\n') //normalize line endings
    .trim()

  return response + '\n';
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接