1

I want to use a regex first character is equal 5 and other string must be number. For example 5046834578 or 5789825364. I used a pattern for to check all string is number but it is not enough to check. How can I do?

var pattern=[0,9];
Hermes
  • 420
  • 7
  • 21

1 Answers1

3

Here's the regex you need:

/^5\d+$/

Anything in / pairs are resolve as regex.

  • ^ means it must starts with the following rule.

  • 5 means 5.

  • \d means any digit.

  • + means previous rule repeat 1 or more than 1 time.

  • $ means it must ends with the previous rule.

Here's the full code:

var regex = /^5\d+?$/;
var tester = '556464622';
var result = regex.test(tester); // true
Paolo
  • 10,935
  • 6
  • 23
  • 45
Hao Wu
  • 12,323
  • 4
  • 12
  • 39