Database Reference
In-Depth Information
Solution
Use a pattern that is similarly broad.
Discussion
To check whether values are empty or nonempty, or consist only of certain types of
characters, the patterns listed in the following table may suffice:
Pattern Type of value the pattern matches
/^$/ Empty value
/./ Nonempty value
/^\s*$/ Whitespace, possibly empty
/^\s+$/ Nonempty whitespace
/\S/ Nonempty, and not only whitespace
/^\d+$/ Digits only, nonempty
/^[a-z]+$/i Alphabetic characters only (case insensitive), nonempty
/^\w+$/
Alphanumeric or underscore characters only, nonempty
12.5. Using Patterns to Match Numeric Values
Problem
You must make sure a string looks like a number.
Solution
Use a pattern that matches the type of number you're looking for.
Discussion
Patterns can be used to classify values into several types of numbers, as shown in the
following table:
Pattern
Type of value the pattern matches
Unsigned integer
/^\d+$/
Negative or unsigned integer
/^-?\d+$/
Signed or unsigned integer
/^[-+]?\d+$/
Floating-point number
/^[-+]?(\d+(\.\d*)?|\.\d+)$/
The pattern /^\d+$/ matches unsigned integers by requiring a nonempty value that
consists only of digits from the beginning to the end of the value. If you care only that
a value begins with an integer, you can match an initial numeric part and extract it. To
 
Search WWH ::




Custom Search