Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Regex to validate a tridion tcm id

Tags:

regex

tridion

Validating a tridion TCM ID using Regex on javascript, looks quiet simple but unfortunately I failed to do this.

I have attempted all the possible ways to validate a tcm id.

Below is the last tried regex:-

(tcm:(\d)+-(\d)+(-16|-4|-64))\W

Here, TCM ID can be component or SG or Page.

Valid tcm id's are

  1. tcm:123-5678-4
  2. tcm:123-5678-64
  3. tcm:123-5678-16
  4. tcm:123-5678

Please suggest the best regex to validate a tcm id.

like image 851
Siva Charan Avatar asked Oct 07 '12 12:10

Siva Charan


1 Answers

The regex xdazz provided is great for matching the examples you gave. But for more generic handling you might want to:

  1. remove the exact cardinalities from, since there may be more or fewer digits in each of the numbers. So /^tcm:\d+-\d+(?:-16|-4|-64)?$/
  2. consider if you want to match every item type and if so, update the last match group accordingly. In the most generic case it can be just /^tcm:\d+-\d+(?:-\d+)?$/
  3. keep in mind that a TCM URI can also refer to a specific version and thus end with -v123. so that would lead to /^tcm:\d+-\d+(?:-\d+)?(?:-v\d+)?$/

Of course that brings you pretty close to Nuno's answer, which has the added advantage that the match groups have readable names.

like image 70
Frank van Puffelen Avatar answered Oct 20 '22 18:10

Frank van Puffelen