1

I found some code online (stackoverflow https://stackoverflow.com/a/5774234/150062) that does exactly what I need. But I can't seem to get it running. I get an error "'/(\\d+)\\s*(second|min|minute|hour)/g' is not a function (evaluating 'regex(s)')";

var timespanMillis = (function() {
  var tMillis = {
    second: 1000,
    min: 60 * 1000,
    minute: 60 * 1000,
    hour: 60 * 60 * 1000 // etc.
  };
  return function(s) {
    var regex = /(\d+)\s*(second|min|minute|hour)/g, ms=0, m, x;
    while (m = regex(s)) {
      x = Number(m[1]) * (tMillis[m[2]]||0);
      ms += x;
    }
    return x ? ms : NaN;
  };
})();

I've never heard of this regex() function either? Is it suppose to be something else?

1
  • 3
    regex is a RegExp object, not a function. The author of that code made a mistake. Commented Aug 21, 2012 at 18:48

2 Answers 2

4

This used to be possible, you can replace the call with exec for exact same mechanism:

m = regex.exec(s)

See http://whereswalden.com/2011/03/06/javascript-change-in-firefox-5-not-4-and-in-other-browsers-regular-expressions-cant-be-called-like-functions/

Sign up to request clarification or add additional context in comments.

Comments

1

i think

regex.match(value)//or regx.exec(value)

is function you are looking for

regex is a RegExp object, not a function. here listing of method and function of Regular Expressions methods and usage

if match is not working than tryout .test() method like this

var match = /sample/.test("Sample text")

or

var match = /s(amp)le/i.exec("Sample text")

2 Comments

Replacing it with that doesn't seem to work... Apparently that code doesn't work at all, nor is it intended to. Apparently it's demo code.
@Steven - than try out .test() method of the object..check the method on given link of answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.