Git Product home page Git Product logo

grunt-regex-extract's Introduction

grunt-regex-extract

Enable the user to extract all matching results of a regular expression and save them to file

Getting Started

This plugin requires Grunt.

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-regex-extract --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-regex-extract');

The "regex_extract" task

Overview

In your project's Gruntfile, add a section named regex_extract to the data object passed into grunt.initConfig().

grunt.initConfig({
  regex_extract: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      // Target-specific file lists and/or options go here.
    },
  },
})

Options

options.regex

Type: String Default value: '(.*|\n*)'

A string value that represents the regular expression to use.

options.modifiers

Type: String Default value: 'ig'

A string value that determines which JavaScript RegExp modifiers to use (such as 'i' and 'g')

options.matchPoints

Type: String Default value: '1'

A comma separated list of match points to determine what part of the regex you want to extract. Note: starts with 1

options.includePath

Type: boolean Default value: 'true'

A boolean value that determines whether the path of the file is to be included in the destination file

options.output

Type: String Default value: ''

Allows complex outputs using using typical dollar-sign grouping syntax (e.g. My $1 complex $2 output)

options.verbose

Type: boolean Default value: 'true'

A boolean value that determines whether the output is printed in the console

Usage Examples

Test File

<!DOCTYPE html>
<html>
	<head>
		<meta charset="ISO-8859-1">
		<title>Hello World</title>
		<script type="text/javascript" src="/js/header-js-one.js"></script>
	</head>
	<header>

	</header>
	<body>
		<h1>Hello World!</h1>
		<p>Foo Bar :)</p>
	</body>
	<footer>
		<script type="text/javascript" src="/js/footer-js-one.js"></script>
		<Script type="text/javascript" src="/js/footer-js-two.js"></script>
		<script type="text/javascript">
			var inlineFooterJsVar = "foo";
		</script>
	</footer>
</html>

Default Options

In this example, the default options are applied but only the regex option is explicitly passed.

Configuration
grunt.initConfig({
    regex_extract: {
		default_options: {
			options: {
				regex : "<script(.*|\n*)>(\s*|\n*)<\/script>"
			},
			files: {
				'test/actual/default_options.txt': ['test/fixtures/example.html']
			}
		}
    },
})
Output
test/fixtures/example.html,type="text/javascript" src="/js/header-js-one.js"
test/fixtures/example.html,type="text/javascript" src="/js/footer-js-one.js"
test/fixtures/example.html,type="text/javascript" src="/js/footer-js-two.js"

Custom Options

In this example, all the default options are overridden

Configuration
grunt.initConfig({
	regex_extract: {
		default_options: {
			options: {
				regex : "<script(.*|\n*)>(\s*|\n*)<\/(\s*|\n*)>",
				modifiers: "g",
				matchPoints: "1,3",
				includePath : false
			},
			files: {
				'test/actual/default_options.txt': ['test/fixtures/example.html']
			}
		}
    },
})
Output
type="text/javascript" src="/js/header-js-one.js",script
type="text/javascript" src="/js/footer-js-one.js",script

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 2014-11-07 v0.1 This release presents the initial version of grunt-regex-extract

License

Copyright (c) 2014 Miren Pau. Licensed under the MIT license.

grunt-regex-extract's People

Contributors

davidmoritz avatar mpau23 avatar

Stargazers

 avatar

Watchers

 avatar

grunt-regex-extract's Issues

New release?

@mpau23 Could you please do an official release of the recently merged enhancements?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.