javascriptregexconfigurationgruntjsgrunt-usemin

Replacing strings using grunt-replace


I would like to replace url("images/ string to some other string e.g. ZZZZZZZZZZZZ using grunt-replace.

I use following configuration:

replace: {
    dist: {
        options: {
            patterns: [
                {
                    match: /"/g,
                    replacement: 'ZZZZZZZZZZZZ'
                }
            ]
        },
        files: [
            {
             expand: true, flatten: true, 
             src: ['dist/app/css/app.css'], dest: 'dist/app/css2/'
            }
        ]
    }
}

I've tried match: /"/g and /images/g and both of them were replaced, but none of those worked:

What am I missing here?

UPDATE I've added files section of my grunt-replace configuration + working /images/g example. I'm using some other Grunt tasks and Grunt replace is last one:

grunt.registerTask('default', ['useminPrepare', 'copy', 'concat', 'uglify', 'cssmin', 'usemin', 'replace']);

But I don't see how it it could break my regex replace. I'm testing my configuration by running command: grunt -v --stack && grep -R "ZZZZZZZZZZZZ" .


Solution

  • First off, your regex101 links point to code using the Python regex library, rather than JavaScript, which is what Grunt uses. Next, the ( and \ characters need to be escaped, but none of the others do. The regexp you're looking for is:

    /url\("images\//g
    

    (see https://regex101.com/r/aY9hY0/1 to play with it)