pertrai1
pertrai1

Reputation: 4318

Grunt match regular expression

I am trying to pull the latest revision number using a regular expression that is not working. Could someone helped a novice regexer with this:

revision: 2020

stdout.match(/^r(\d+)/m)[1])

Thank you for any help you can give

EDIT

I should have put my whole task here so all would know what I am trying to do.

grunt.registerTask('version', 'Updating to latest revision', function () { var done = this.async(); grunt.util.spawn({ cmd: 'svn info', args: ['-r HEAD'] }, function (err, result, code) { var output = result.stdout; if (output) { var revision = result.stdout.match(/\d+/)[0]; grunt.file.write('web/version2.js', 'var UIVERSION = ' + revision); } done(); }); } );

Upvotes: 0

Views: 216

Answers (1)

Andy
Andy

Reputation: 63524

You should just be able to do \d+, and then pull the first element, like:

var str = 'revision: 2020';
var regex = /\d+/
var number = str.match(regex)[0]; // '2020'

DEMO

And if you want it as an integer rather than a string, just add a + to the expression:

var number = +str.match(regex)[0]; // 2020

Upvotes: 1

Related Questions