S
S
svilkov872018-09-20 10:42:59
JavaScript
svilkov87, 2018-09-20 10:42:59

How to solve such a problem with a regular expression?

We need a regular expression for which the following occurrences will be TRUE: "money_1", "money_2", "money_3", "money_4_a", "money_40_a", "money_{number}_{word}". Where "number" can be any number and "word" can be any character set.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-06-03
@svilkov87

const reg = /^money_\d+(_\w+)?$/;

// подходят
console.log([
  'money_1',
  'money_22',
  'money_3_a',
  'money_0_xxx666'
].map(n => reg.test(n)));

// не подходят
console.log([
  'honey_1',
  'money_q2',
  'money_3~a',
  '!money_0_xxx666'
].map(n => reg.test(n)));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question