The regular expression of js, the value must be composed of numbers and letters.

For the regular expression of

js, the value must be composed of numbers and letters. How to write it, Baidu searches for numbers or letters

.
Mar.23,2021

this single item is actually quite troublesome, in fact, it is simple to cooperate with the judgment
for example, first test whether it is composed of numbers or letters, and then test whether it contains both numbers and letters, you can judge.

/ ^ (?! [0-9] + $) (?! [a-zA-Z] + $) [0-9A-Za-z] * $/ . This can be tried and the number of digits can be determined, such as
/ ^ (?! [0-9] + $) (?! [a-zA-Z] + $) [0-9A-Za-z] {8pm 20} $/ , which requires 8-20 digits only by letters and numbers. And contains both letters and numbers

< hr >

where
(?! [0-9] + $) predicts that the position is not all followed by numbers
(?! [a-zA-Z] + $) predicts that the position is not all followed by letters


// 
var reg = /^[^\d]*$|^[^a-zA-Z]*$|[^\da-zA-Z]/

var str = "12345";
if (reg.test(str)) {
    console.log("");
    throw new Error("");
}

Note that the above regular match is successful, which means that the value is illegal

Menu