Classify order identifiers
Require ORD- followed by exactly four ASCII digits for an accepted identifier. This guide uses JavaScript to check the entire input.
Describe the format
^ORD-[0-9]{4}$
Use no flags. ^ starts the match at the beginning of the input, and {4} requires four digits.
$ requires the end of the input because multiline mode is disabled.
Verify ordinary cases
| Input | Pattern finds a match? | Strict format accepted? |
|---|---|---|
ORD-0042 | Yes | Yes |
ORD-9999 | Yes | Yes |
ORD-42 | No | No |
ORD-00000 | No | No |
XORD-0001 | No | No |
ord-0042 | No | No |
| Empty input | No | No |
ORD-0042 followed by a newline | No | No |
A case-insensitive flag changes the lowercase result. A multiline flag changes how the anchors treat lines. Keep both flags disabled for this format check.
Require the entire input
With this pattern and no flags, test() rejects the trailing newline.
You can also compare the match with the entire input:
const pattern = /^ORD-[0-9]{4}$/
function isOrderIdentifier(value) {
const match = pattern.exec(value)
return match !== null && match[0] === value
}
console.log(isOrderIdentifier('ORD-0042')) // true
console.log(isOrderIdentifier('ORD-0042\n')) // false
console.log(isOrderIdentifier('ORD-42')) // false
The comparison does not remove spaces or line endings. If your application removes spaces or line endings first, verify that step too.
Use the identifier in an application
The pattern checks the format, not whether the order exists.
For example, ORD-9999 passes this format check even if the database has no corresponding order.
The does match unit-test expectation checks whether the pattern finds a match. Also test the full-input comparison in your application.