JavaScript Minify HTML正则表达式 [英] JavaScript Minify HTML Regex

查看:62
本文介绍了JavaScript Minify HTML正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript正则表达式是什么,以最小化HTML的内容.请注意,我只想删除> 2且下面没有的空格.

What would the JavaScript regex be to minify contents of HTML. Note that I only want to remove spaces that are >2 and nothing below.

我也想将双引号" "替换为单引号' '

I also want to replace single quotation marks ' ' with double " "

这是我到目前为止的结果,尽管我猜想有一种更有效的方法:

This is what I got so far, although I'm guessing there's a more efficient way of doing this:

var findSpaces = content.match(' ') >= 2;
var findQuotes = content.match(" ' ");

content.replace(findSpaces, "" );

content.replace(findQuotes, ' " ' );

请没有j查询

推荐答案

var s = `

    <div   value="a"     class="a b"   id="a">
      <div>
        foo bar  
        <br><br>
        <span>baz</span>   <i>a</i>  
      </div>
    </div>
`

console.log(
  s.replace(/\s{2,}/g, ' ').replace(/\'/g, '"')
)

应该为您完成工作

这篇关于JavaScript Minify HTML正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆