简体   繁体   English

我可以使用什么正则表达式来匹配HTML文档中的CSS和JavaScript链接?

[英]What regex can I use to match CSS and JavaScript links in an HTML document?

I'm using a simple gulp task for "minifying" my CSS and JS files. 我正在使用一个简单的gulp任务来“缩小”我的CSS和JS文件。 The task adds a .min suffix to the name of the minified files. 该任务将.min后缀添加到缩小文件的名称中。 I'd like to update the HTML to point to the new, compressed files eg: 我想更新HTML以指向新的压缩文件,例如:

Change this: 更改此:

<link ...  href="...some_name.css ..." ...>

To this: 对此:

<link ...  href="...some_name.min.css ..." ...>

I use gulp-string-replace plugin , but I can't figure out the right regex to target JS and CSS links. 我使用的是gulp-string-replace插件 ,但是我找不到适合JS和CSS链接的正则表达式。 I don't want to hardcode filenames and I obviously can't just match for the strings .css or .js . 我不想对文件名进行硬编码,显然我不能只匹配字符串.css.js

Sample tasks: 示例任务:

var replace = require('gulp-string-replace');

gulp.task('replace_1', function() {
  gulp.src(["./config.js"]) // Any file globs are supported
    .pipe(replace(new RegExp('@env@', 'g'), 'production'))
    .pipe(gulp.dest('./build/config.js'))
});

gulp.task('replace_2', function() {
  gulp.src(["./index.html"])
    .pipe(replace(/version(={1})/g, '$1v0.2.2'))
    .pipe(gulp.dest('./build/index.html'))
});

gulp.task('replace_3', function() {
  gulp.src(["./config.js"])
    .pipe(replace(/foo/g, function () {
        return 'bar';
    }))
    .pipe(gulp.dest('./build/config.js'))
});

这将解决您的问题,但将包括所有CSS文件链接。

replace(/href=\"(\S*)\.css\"/gi, 'href="$1.min.css"')

The usual way to do this would be to incorporate something like the gulp-processhtml plugin in your workflow. 通常的方法是在工作流程中加入gulp-processhtml插件之类的东西。 It is designed to do exactly what you are doing. 它旨在完全按照您的工作来做。

  <!-- build:css style.min.css -->
  <link rel="stylesheet" href="css/style.css">
  <!-- /build -->

becomes simply 变得简单

<link rel="stylesheet" href="style.min.css">

Here is a gulp 4.0 task I use when moving a development version of the html to a deployment directory: 这是将html的开发版本移动到部署目录时使用的gulp 4.0任务:

var stripComments = require("gulp-strip-comments");
var modifyHTMLlinks = require("gulp-processhtml");
var addVersionString = require("gulp-version-number");
var print = require('gulp-print').default;

function processHTML() {
  return gulp.src(paths.html.src)
    .pipe(print())

    // modifyHTMLlinks: remove browserSync script link
    // update css/js links to .min.css or .min.js
    .pipe(modifyHTMLlinks())

    .pipe(stripComments.html(stripOptions))

    // add ?v=dateTime stamp to css and js links
    .pipe(addVersionString(versionConfig))
    .pipe(gulp.dest(paths.html.deploy));
}

This seems much more foolproof than messing with regex's but perhaps you have some reason to avoid such a plugin? 这似乎比使用正则表达式要简单得多,但也许您有某些理由要避免使用此类插件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 html 文档末尾的一堆 p 标签与 javascript 正则表达式匹配? - How can I match a bunch of p tags at the end of an html document with javascript regex? Javascript匹配正则表达式链接 - Javascript match regex links 如何正确地将CSS和JavaScript添加到HTML文档? - How can I properly add CSS and JavaScript to an HTML document? 如何在JavaScript中的RegEx中使用变量,以使用“ ^”和“ $”进行完全匹配? - How can I use a variable with RegEx in JavaScript for an exact match using “^” and “$”? JavaScript正则表达式以大写形式匹配链接 - JavaScript regex to match links with uppercase 如何在没有document.getElementById限制的情况下使用javascript将链接插入HTML - How can I insert links into HTML using javascript without the limitations of document.getElementById 如何将导航栏链接变成带有 HTML CSS 和 JavaScript 的下拉菜单? - How can I turn my navbar links into a dropdown menu with HTML CSS and JavaScript? 使用HTML / CSS / Javascript在新页面中打开链接 - Use HTML/CSS/Javascript to open links in new pages 我可以使用什么JavaScript或CSS库来实现此下拉效果? - what javascript or css libraries can i use to achieve this dropdown effect? 我可以使用什么 javascript 正则表达式来删除 HTML 代码? - What javascript regular expression can I use to remove HTML code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM