The CreatorCon Call for Content is officially open! Get started here.

Regex is not working as expected

SAS21
Tera Guru

Regex should mask if the card number is exactly 15 digits(including the first two)  - not less than that not more than that

Here is the regex 

var matches = testcases.match(new RegExp('(?:^|(?<=[^0-9]))37(?:[ \t-]*\\d){13}', 'gj'));

 

but the above is masking the number that has 16 digits as well.

 

Appreciate the Help

 

1 ACCEPTED SOLUTION

Jitendra Diwak1
Kilo Sage

Hi @SAS21

 

Here is below code for the same. 

 

var matches = testcases.match(new RegExp('(?:^|(?<=[^0-9]))37(?:[ \t-]*\\d){13}(?![0-9])', 'gj'));

 

Please accept my solution if it resolves your issue and thumps 👍 up 

 

Thanks 

Jitendra 

Please accept my solution if it works for and thumps up.

View solution in original post

7 REPLIES 7

Robbie
Kilo Patron
Kilo Patron

Hi @SAS21,

 

Just to make things a little clearer, can you provide an example of both a valid and non valid card number please.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.


Thanks, Robbie

Hi Robbie , 

 

Here is the code i am trying

 

var testcases = 'a3711111111111\na37222222222222\n3711 11111 1111\n3711---11111-1111\nMycardnumberis373737373737373\nThis should not match: 1234567812345';
var matches = testcases.match(new RegExp('(?:^|(?<=[^0-9]))37(?:[ \t-]*\\d){13}', 'gj'));
gs.info('Test Cases:\n' + testcases + '\n');
gs.info('Matches:');
for(var i in matches)
gs.info(matches[i]);

Hi @SAS21,

 

Thanks for providing the script. Running it via a background script the match appears on "373737373737373"currently.

What I was after was explicit examples of what are valid card numbers where you want the match, and examples where it should not match.

 

For example:

- "A12345678901234" - Should this match? (15 chars in total of which after the initial letter, 14 numbers follow)

- "A123456789012345" - Should this match? (16 chars in total of which after the initial letter, 15 numbers follow)

- "AB123456789012345" - Should this match? (17 chars in total of which after the initial letter, 15 numbers follow)

 

I assume spaces and other 'special characters' are not allowed. Please confirm the above as to which ones are valid and if spaces / special chars are not allowed.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.


Thanks, Robbie

 

Community Alums
Not applicable

Hi @SAS21 ,

Try using :

@"\b(?:\d[ -]*?){11,15}(?=\d{15}\b)";