How to create a four-digit comma-separated regular expression

Asked 1 years ago, Updated 1 years ago, 320 views

I'm creating a regular expression under a four-digit comma-separated condition, but if I match more than one, I'd like to know how to create a four-digit comma-separated expression that becomes False if there are other non-matching conditions.

Example)
1234,4321 (OK)
1234,212(NG)
1223, 2344, (NG)

javascript regular-expression

2022-10-08 01:00

1 Answers

'^\\d{4}(,\\d{4})*$' is a regular expression that allows only four-digit numbers, including the first number.

sample code

<html><body>
<script>
window.onload=(event)=>{
  letre=newRegExp('^\\d{4}(,\\d{4})*$');
  letarr = ['1234,4321,'1234,212', '1223,2344,', '123,1234', '1000, 2000, 3000', '9999' ];
  err.forEach(s=>{
    console.log('%s is %s.', US>%s,re.test(s)?'OK':'NG');
  });
};
</script>
</body></html>

Run Results

 1234,4321 is OK.
1234, 212 are not allowed.
1223, 2344, are not allowed.
123 and 1234 are not allowed.
1000, 2000 and 3000 are OK.
9999 is OK.


2022-10-08 01:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.