0

I expect true return value that match condition below :

  • Only binary number (0 or 1)
  • Length not more than 8

I'm new to regex, i have googling and read JS RegExp from https://www.w3schools.com/Js/js_regexp.asp but still i don't get it.

I've tried

/[0-1]$/

but still didn't match the condition above

I expect boolean return from regex test if there is contain no other number except 0 or 1 and length not more than 8.

data: {
  binRegExp: /[0-1]$/,
  isBinary: false,
  binNum: '', // get value from user input
},
computed: {
  inputCheck(){
    return this.isBinary = this.binRegExp.test(this.binNum)
  }
}

code above is vue js

Dharman
  • 30,962
  • 25
  • 85
  • 135
Devz
  • 23
  • 1
  • 3

2 Answers2

1

this will takes 1 to 8 [1-0]

const regex = /^[0-1]{1,8}$/


const text = "1010010"

console.log(regex.test(text));
Aziz.G
  • 3,599
  • 2
  • 17
  • 35
0

Try

/^[01]{1,8}$/

let strings = ['11110001','1111000111','11112001'];

strings.forEach(x=> /^[01]{1,8}$/.test(x) ? console.log(x,'pass') : console.log(x,'not pass'))
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345