1

Is there a function in javascript which checks if entire string matches some regular expression

'00'.match(/^0|([1-9][0-9]*)$/g) for example returns ['0']

I know I can simply check if the matched part is equal to the string, but I'm just curious if such function already exists

edit:

'01'.match(/^0|1$/g) returns ['0', '1'] and '01'.match(/^(0|1)$/g) returns null as expected.

I though | has precedence over ^ or $. Can someone explain what /^0|1$/g actually matches?

1
  • 2
    /^0|1$/g matches a 0 at the beginning of the string or a 1 at the end of the string. Commented Nov 25, 2018 at 22:02

1 Answer 1

1

I don't know what you exacty want to achieve. I suppose you want to match eiter zero or a number, not beginning with zero. Then try this:

'00'.match(/^(0|[1-9][0-9]*)$/g)

This does not match '00', but all the other cases.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.