0

I'm trying to create a regex that will work as the following pattern:

tariff_0 - false; tariff - false

tariff_0_0 - true; tariff_100_50 - true

I've read that we can trigger digits with [0-9]+ expression, but unfortunately, it is not working in my case.

Here is a link to my RegEx: RegEx

And my Regex: /tariff_[0-9]+_[0-9]+/

Karen
  • 1,249
  • 4
  • 23
  • 46

3 Answers3

0

The issue with the current regular expression is that it stops finding matches at the first encounter.

Using the global flag would find as many matches as there are.

ex :

/tariff_[0-9]+_[0-9]+/g

Here, g is the global flag

Prashanth Benny
  • 1,523
  • 21
  • 33
0

you can try this pattern: /^tariff(_[0-9]){2,}$/

David
  • 61
  • 4
0

var str1 = 'tariff_0';
var str2 = 'tariff';
var str3 = 'tariff_0_0';
var str4 = 'tariff_100_50';
var str5 = 'tariff_100_500';
var patt = new RegExp("tariff_([0-9])+_([0-9])");
console.log(patt.test(str1));
console.log(patt.test(str2));
console.log(patt.test(str3));
console.log(patt.test(str4));
console.log(patt.test(str5));

Note:- This Regex will first check string contains _ >>> number is between [0-9] >>> the same process as above...

Parth Raval
  • 4,097
  • 3
  • 23
  • 36