0

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???
}
3
  • you know how long the pattern is so you can use string.substring() Commented Aug 14, 2014 at 21:17
  • Do you search in cardData ? in input ? Commented Aug 14, 2014 at 21:19
  • sorry, incorrect variable name in question - edited Commented Aug 14, 2014 at 21:54

1 Answer 1

2

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"
}
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.