ProgramWriter
ProgramWriter

Reputation: 283

Partial RegExp match

I want to write a JavaScript input key validator. I want to use regular expressions for this purpose. When user presses a key - input text field is checked that his value satisfy some regular expression, and if it isn't then user input is rejected.

Is it possible to catch a match when regular expression matches only part of expression?

Just for better understanding of question:
Regular expression: ([0-9]{3})([0-9Xx]+)

"" matches
"0" matches
"01" matches
"01c" not matches
"014" matches
"0149" matches
"0149x" matches
"0149xD" not matches
"0149xDX" not matches
"0149xX" matches

and so on...

Upvotes: 2

Views: 579

Answers (2)

alpha-mouse
alpha-mouse

Reputation: 5003

Why not having a one regular expression for text being edited and another for the final text. So if you can specify what text is appropriate as a "half input" you can master a regex for it.

Upvotes: 0

CyberDem0n
CyberDem0n

Reputation: 15036

^([0-9]{0,2}|[0-9]{3}[0-9Xx]*)$

Upvotes: 2

Related Questions