0

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(); }); } );

pertrai1
  • 4,146
  • 11
  • 46
  • 71

1 Answers1

1

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
Andy
  • 61,948
  • 13
  • 68
  • 95
  • thank you for the demo. I a sorry that I had to edit my question so it is clearer what I am trying to solve – pertrai1 Apr 09 '15 at 16:08
  • @pertrai1, your edit didn't make it clearer for me. Are you saying that my code doesn't work. I see you've added it to that Grunt task. Is there still an outstanding issue? – Andy Apr 09 '15 at 16:14
  • sorry it was a mistake on my end. Thank you – pertrai1 Apr 09 '15 at 16:21
  • Don't know if you would be willing to take the time in this comment section, what exactly does putting the '+' in front of str do? What is that called so I can research it? Thank you for any help you can give. – pertrai1 Apr 10 '15 at 08:25
  • It just coerces what was a string into an integer. See [this question](http://stackoverflow.com/questions/221539/what-does-the-plus-sign-do-in-return-new-date). – Andy Apr 10 '15 at 09:28