skip to Main Content

Day 353 – still stuck with RegEx

I am just simply not getting through with jQuery Regex

I would like to test syntax 9.3x1.3 where the nums can be any amount like 12.5x11.3 is also accepted, the important part is that it must be one or two num dot num x one or two num dot num

I online tested the /[0-9].[0-9][x] [0-9].[0-9] version online but doesnt seems to be working in RL. RegEx seems to be an out-of-world thing to me…

2

Answers


  1. I hope this works for you d is a convenience syntax for [0-9]

    /^d{1,2}.dxd{1,2}.d$/

    This should match the syntax described: one or two num dot num x one or two num dot num

    enter image description here

    Edit: added test

    const correctText = '12.3x9.1';
    const incorrectText = '120.3x9.1';
    const matcher = /^d{1,2}.dxd{1,2}.d$/;
    
    console.log('correctText match result: ', matcher.test(correctText)); // should output true
    console.log('incorrectText match result: ', matcher.test(incorrectText)); // should output false
    Login or Signup to reply.
  2. Your regex was in the right direction, it only missed the valid length of each section.

    [0-9] means one digit is allowed. Need to use {min, max} to set valid length.

    It should be like this :

    /^[0-9]{1,2}.[0-9][x][0-9]{1,2}.[0-9]$/
             ^                  ^
             Here we say that length can be 1 or 2
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search