Reputation: 2194
I have a filename that will be something along the lines of this:
Annual-GDS-Valuation-30th-Dec-2016-082564K.docx
It will contain 5 numbers followed by a single letter, but it may be in a different position in the file name. The leading zero may or may not be there, but it is not required.
This is the code I come up with after checking examples, however SelectedFileClientID
is always null
var SelectedFileClientID = files.match(/^d{5}\[a-zA-Z]{1}$/);
I'm not sure what is it I am doing wrong.
Edit:
The 0
has nothing to do with the code I am trying to extract. It may or may not be there, and it could even be a completely different character, or more than one, but has nothing to do with it at all. The client has decided they want to put additional characters there.
Upvotes: 1
Views: 5425
Reputation: 3864
Try - 0?\d{5}[azA-Z]
As you mentioned 0
may or may not be there. so 0?
will take that into account.
Alternatively it can be done like this. which can match any random character.
(\w+|\W+|\d+)?\d{5}[azA-Z]
Upvotes: 0
Reputation: 626747
There are at least 3 issues with your regex: 1) the pattern is enclosed with anchors, and thus requires a full string match, 2) the d
matches a letter d
, not a digit, you need \d
to match a digit, 3) a \[
matches a literal [
, so the character class is ruined.
Use
/\d{5}[a-zA-Z]/
Details:
\d{5}
- 5 digits[a-zA-Z]
- an ASCII letterJS demo:
var s = 'Annual-GDS-Valuation-30th-Dec-2016-082564K.docx';
var m = s.match(/\d{5}[a-zA-Z]/);
console.log(m[0]);
Upvotes: 1
Reputation: 3127
All right, there are a few things wrong...
var matches = files.match(/\-0?(\d{5}[a-zA-Z])\.[a-z]{3,}$/);
var SelectedFileClientID = matches ? matches[1] : '';
So:
.match()
^
\d
{1}
for your letters: the square bracket content is enough as it will match one, and only one letter.Hope this helps!
Upvotes: 0