1

How I can check that my variable match with this pattern A|0, B|0, C| ... Z|0 in jQuery?

The pattern is [one letter]|0

if (myVar.indexOf((/^[A-Z]+$/+'|0') > -1){
  // true;
}
1
  • 1
    Note that jQuery is a framework primarily intended for amending the DOM. As you're working with regular expressions here you just need plain JS. I've retagged the question for you. Commented Mar 3, 2020 at 8:55

2 Answers 2

1

Given that input your regex should look like this: /^[A-Z]\|0$/. [A-Z] matches the first uppercase character, then the pipe is escaped using \| before looking for the final 0 at the end of the input string. From there you can use test() to check whether the input meets that expression.

['A|0', 'A0', 'B|0', '0|C', 'd|0'].forEach(item => {
  var result = /^[A-Z]\|0$/.test(item);
  console.log(item, result);
})

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

Comments

1

This following regex can match any integer after |

['A|0', 'A0', 'B|0', '0|C', 'd|0'].forEach(str => {   
   var result =/^[A-Za-z]\|(\d+)$/.test(str);   
   console.log(str, result); 
})

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.