清理HTML标记属性

时间:2010-11-03 21:14:02

标签: javascript regex html-parsing tag-soup

我需要使用JavaScript来浏览大量HTML,将属性引用语法调整为双引号。我不需要担心像“禁用”或“已选择”这样的仅限密钥属性。

这是我目前的测试用例:

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 个答案:

答案 0 :(得分:2)

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

或(一个替代):

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

答案 1 :(得分:1)

我知道这是一个迟到的答案,但如果你总是可以使用sanitize-html它是为节点编写的,但很确定你可以对库(或你的代码)运行browserify

注意,它使用lodash,所以如果您已经在使用它,那么您可能需要调整包。

这个例子比你正在寻找的更多......我使用这个库来清理输入代码,从这里转换为存储在db中的markdown,我通过marked重新补充。 / p>

// 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';
}