How to check if a string contains only letters and numbers using RegEx in Node.js

1 Answer

0 votes
function isAlphanumeric(str) {
    // Define the regular expression for alphanumeric characters
    const alphanumericRegex = /^[a-zA-Z0-9]+$/;

    // Use the test() method to check if the string matches the pattern
    return alphanumericRegex.test(str);
}

const str = "Yv09wr87KO1zz765M";

if (isAlphanumeric(str)) {
    console.log("The string contains only letters and numbers.");
} else {
    console.log("The string contains characters other than letters and numbers.");
}



   
/*
run:
    
The string contains only letters and numbers.
       
*/

 



answered Mar 26 by avibootz
...