user11759028
user11759028

Reputation: 167

How to match value with Regex in angular

I need to match regex and need to put condition based on result.. What I have tried is

var value1="4111111111111111"
    const str="^4[0-9]{12}(?:[0-9]{3})?$}"
var result=value1.match(str)
console.log(result)

Here I am getting value as null..

Upvotes: 5

Views: 16366

Answers (2)

miladfm
miladfm

Reputation: 1526

If you pattern is somthimg like that: 4111111111111111 or 4111111111111111

then use this code:

const str="^4[0-9]{12}([0-9]{3})?$";

'4111111111111'.match(str)
'4111111111111111'.match(str)

Upvotes: 0

Avinash Dalvi
Avinash Dalvi

Reputation: 9291

Try this :

var value1="4111111111111111"
var pattern = new RegExp('^4[0-9]{12}(?:[0-9]{3})?$}');
var result=pattern.test(value1);
console.log(result);

This will return either True or False

Upvotes: 10

Related Questions