I have an input which can have up to 50 ASCII characters with a hidden pattern that I need to find. The pattern is the letter A (case insensitive) followed by 8 digits. Only the first pattern (if exists) needs to be found. So an example input can be lkjs#$9234A12345678*)(&kj
How do I do this in JavaScript?
var input = 'lkjs#$9234A12345678*)(&kj';
var regex = '[Aa][0-9]{8}';
var index = input.search(regex);
if (index >= 0) {
//found pattern - but how to extract it???
}
You should use match :
var match = input.match(/a\d{8}/i); // yes, that's an equivalent regular expression
if (match) { // if a match is found
var str = match[0];
// here you go, str is your "substring"
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments