Regular matching can only be numeric letters, where do the beginning and end match?

regular matching can only be numeric letters, where the start and end match.
let reg2=/ ^ [a-zA-Z0-9] + $/

        let reg1=/[a-zA-Z0-9]+/;//
        let reg2=/^[a-zA-Z0-9]+$/;//?
         
        console.log(reg1.test(""));//false
        console.log(reg1.test("a-"));//true
        console.log(reg1.test("13232-3213"));//true
        console.log(reg1.test("423432432"));//true

        console.log(reg2.test(""));//false
        console.log(reg2.test("a-"));//false
        console.log(reg2.test("13232-3213"));//false
        console.log(reg2.test("423432432"));//true
May.28,2021

^ matches the beginning of the string, matches a position;
$ matches the end of the string, matches a position;
/ ^ [a-zA-Z0-9] + $/ means to match the beginning of the string first, then 1 to more letters or numbers, and then the end of the string. To sum up, the whole string is made up of 1 to more letters and numbers.

you can use the match method of the string to further check the match. test only returns true or false , and does not provide a specific string for the match:

let reg1=/[a-zA-Z0-9]+/g;
let reg2=/^[a-zA-Z0-9]+$/g;
 
'13232-3213'.match(reg1) // ["13232", "3213"]
'423432432'.match(reg1) // ["423432432"]

'13232-3213'.match(reg2) // null
'423432432'.match(reg2) // ["423432432"]

means that a string to match this regular must be all numbers and letters, from the beginning to the end are letters and numbers


regular match you currently need to match the entire string, if the string matches the regular return true, otherwise return false.

Menu