JavaScript javascript regex conditional number formatting javascript regex conditional replace,javascript regex conditional group,javascript regex

I have a task that has a few rules. Basically, I am separating a number into groups of 3. If the last group only has 1 digit in it, then the last 2 groups have 2 digits instead of 1 group of 3 and 1 group of 1.

For example:

123456789 = 123-456-789
12345678901 = 123-456-789-01
1234567 = 123-45-67

My regex so far has been:

serial.match(/\d{3}|\d+/g)

where serial is a string passed in via function

function format(serial) {
    return serial.replace('/-|/s/g', '').match(/\d{3}|\d+/g).join('-');
}

This gets me the majority of the way there, just doesn't split properly when the last match is {4}.

My progress via fiddle: https://jsfiddle.net/dboots/2mu0yp2b/2/

Answer:1

I am trying to authenticate a user using the Basic Authentication protocol in Jquery but it keeps giving error:"XMLHttpRequest cannot load https://api.credly.com/v1.1/authenticate. Request header ...

I am trying to authenticate a user using the Basic Authentication protocol in Jquery but it keeps giving error:"XMLHttpRequest cannot load https://api.credly.com/v1.1/authenticate. Request header ...

  1. basic auth work
  2. basic auth not working in postman
  3. basic auth not working
  4. basic auth not working apache
  5. basic auth not working kubernetes
  6. basic auth doesn't work
  7. basic auth header not working
  8. auth basic not working nginx
  9. basic auth post not working
  10. traefik basic auth not working
  11. axios basic auth not working
  12. htaccess basic auth not working
  13. auth_basic off not working
  14. apache2 basic auth not working
  15. basic auth in url not working
  16. curl basic auth not working
  17. express-basic-auth not working
  18. grafana basic auth not working
  19. swagger basic auth not working
  20. flask basic auth not working

I was wondering if there is a better way to do this: var books = [ { title: 'Some Book', author: { firstName: 'John', lastName: 'Doe' } }, { title: ...

I was wondering if there is a better way to do this: var books = [ { title: 'Some Book', author: { firstName: 'John', lastName: 'Doe' } }, { title: ...

I have searched in many ways to get the solution of this problem but not found any working solution. I have visited many questions on this platform but not found any working solution. I know that i ...

I have searched in many ways to get the solution of this problem but not found any working solution. I have visited many questions on this platform but not found any working solution. I know that i ...

I am trying to make a append menu on click div. But i have one question here. How can i remove appended elements after second click. Anyone can help me in this regard ? This is the DEMO page from ...

I am trying to make a append menu on click div. But i have one question here. How can i remove appended elements after second click. Anyone can help me in this regard ? This is the DEMO page from ...

  1. jquery click append
  2. jquery click append element
  3. jquery click append div
  4. jquery click append html
  5. jquery append click event
  6. jquery append click not working
  7. jquery append click handler
  8. jquery append click function
  9. jquery click after append
  10. jquery button click append div
  11. jquery after append click not working
  12. jquery click event after append
  13. jquery click event on append element
  14. jquery click function after append
  15. jquery append add click event
  16. jquery click doesn't work after append
  17. jquery click on element after append
  18. jquery bind click after append
  19. jquery trigger click after append