1

Kindly note, I tried some suggestions from other SO post about regexp, none helped!!! Coming to the question -

I have JSON data like this (original: https://api.myjson.com/bins/fvzpp):

{
  VISA: {
    cardPattern: "/^4/",
    cardNumberLength: 16,
    cvv: "required",
    cvvLength: 3,
    displayText: "Visa"
  },
  MASTERCARD: {
    cardPattern: "/^5[1-5]/",
    cardNumberLength: 16,
    cvv: "required",
    cvvLength: 3,
    displayText: "Master"
  },
  MAESTRO: {
    cardPattern:
      "/^(50|63|66|5[6-8]|6[8-9]|600[0-9]|6010|601[2-9]|60[2-9]|61|620|621|6220|6221[0-1])/",
    cardNumberLength: 19,
    cvv: "optional",
    cvvLength: 4,
    displayText: "Maestro"
  }
}

Please see the cardPattern property which is in string format. How do i make use of the same to test a string(say 445). For example, "/^4/".test(mysttring) does not work. And also var reg = new RegExp("/^4/") returns a weird //^4// which will never match mystring. How to handle such a response for regex? Just to be more explicit, how to handle response['VISA'].cardPattern.test(4545) which should have worked, but does not!

And also, The response is not JSON format, and is object inside object. What is the best way to parse such a response? I tried for-in loop, but that returns 'VISA', 'MASTERCARD' and 'MAESTRO'(the strings) which is not what I want.

3
  • try like this /^4/.test(...) or new RegExp(“^4”) Commented Oct 8, 2017 at 15:58
  • @AdarshMohan - That's not the point. How to handle from the call example - response['VISA'].cardPattern.test(4545)? It should have worked, but will not!!! Commented Oct 8, 2017 at 16:00
  • 1
    remove the leading and trailing slashes and then passing as an argument to RegExp like.. new RegExp(response.VISA.cardPettern.replace(“/“,””).replace(/\/$/,””)).test(4545); maybe you can combine the two replace with a single regex Commented Oct 8, 2017 at 16:05

2 Answers 2

2

You'll have to use the new RegExp() constructor to parse the regexp first.

However, you'll have to strip the leading and trailing slashes:

function parseCardPattern(cardPattern) {
  return new RegExp(cardPattern.substr(1, cardPattern.length - 2));
}

You can now use the regexp object for testing:

const visaPattern = parseCardPattern(response['VISA'].cardPattern);
console.log(visaPattern.test('4242424242'));  // => True
Sign up to request clarification or add additional context in comments.

Comments

0

If I don't misunderstood your question. Grab cardPattern like this json.VISA.cardPattern and then try like this

let json = {"VISA":{"cardPattern":"/^4/","cardNumberLength":16,"cvv":"required","cvvLength":3,"displayText":"Visa"},"MASTERCARD":{"cardPattern":"/^5[1-5]/","cardNumberLength":16,"cvv":"required","cvvLength":3,"displayText":"Master"},"MAESTRO":{"cardPattern":"/^(50|63|66|5[6-8]|6[8-9]|600[0-9]|6010|601[2-9]|60[2-9]|61|620|621|6220|6221[0-1])/","cardNumberLength":19,"cvv":"optional","cvvLength":4,"displayText":"Maestro"}};
const regex = json.VISA.cardPattern;
const str = `4545`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }

    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

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.