0

Regular expression not able to read complete string, only working correct with single character.

var abc = "ab";
var patter = /^([a-z0-9A-Z])$/;

 if (patter.test(abc)) {
    console.log('yes');
 } else {
   console.log('no');
} 
1

1 Answer 1

2

You must set a quantifier when you don't want just one character.

Add a * to match zero or more character (or a + if you want to be sure there's at least one character);

var patter = /^[a-z0-9A-Z]*$/;

Note that I removed the parentheses : they're useless with the test method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.