0

My regex is next

/^(\.\w*)|(\d*\.?\d*)$/

it should works well for float numbers (123.23, 12., .56) and any words that starts from dot.
I was confused when
/^(\.\w*)|(\d*\.?\d*)$/.test("qweasdzxc"); // return true
but without OR:

/^(\.\w*)$/.test("qweasdzxc"); // return false   
/^(\d*\.?\d*)$/.test("qweasdzxc"); // return false

On RegexPal all works well

1
  • 1
    The equivalent expressions without OR are ^(\.\w*) and (\d*\.?\d*)$ and the latter is true. Commented Mar 18, 2013 at 12:13

2 Answers 2

2

It should be

/^((\.\w*)|(\d*\.?\d*))$/

You need to wrap both the OR conditions within another (..).

Other wise it is interpreted as ^(\.\w*) or (\d*\.?\d*)$ instead of ^ and ( (\.\w*) or (\d*\.?\d*) ) and $.

Demo: Fiddle

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

Comments

1

Try this

/^((\.\w*)|(\d*\.?\d*))$/.test("qweasdzxc"); // false
/^((\.\w*)|(\d*\.?\d*))$/.test(".5"); // true

You must encapsulate regex condition (A|B).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.