0

I have a function that finds the class name fc-id## which could be ex: fc-id3 or fc-id14

I turn this into fc-day##. The problem is it something like fc-id14 becomes fc-day1 ... It seems only the first digit is parsed...

$(mondays).each(function () {
    var num = this.className.split(' ')[0].match(/fc-id(\d)/)[1];
    var clsnme = '.fc-day' + num;

    $(this).addClass('monday');
    $(clsnme).addClass('monday');
});

How can I get it to parse the entire number?

2
  • 1
    you are using the wrong regex, use /fc-id(\d+)/ or /fc-id(\d*)/, first checks one or more digit while second checks 0 or more. Decide which one to use Commented Apr 2, 2013 at 16:38
  • var num = this.className.split(' ')[0].replace('fc-id',''); do you really need a regular expression for this ? Commented Apr 2, 2013 at 16:42

2 Answers 2

2

It's giving you exactly what you're asking for. \d matches a single digit. If you want to match multiple digits, you need to use a quantifier like * (zero or more) or + (one or more):

/fc-id(\d+)/
Sign up to request clarification or add additional context in comments.

Comments

2

Match more than one number (\d):

var num = this.className.split(' ')[0].match(/fc-id(\d+)/)[1];

\d matches for exactly one digit, \d+ matches one or more digits.

Comments

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.