简体   繁体   English

从URI Regex获取Tridion项目ID

[英]Get Tridion Item ID with from URI Regex

If I have a Tridion URI like this 'tcm:1-23-8' and I want to get 23 with a Regular Expression. 如果我有一个像'tcm:1-23-8'这样的Tridion URI,我想用正则表达式得到23。

The following works, but I know there is a better way. 以下是有效的,但我知道有更好的方法。 tcm: and '-8' are always there. tcm:和'-8'总是在那里。 The parts that change are 1 and 23. 更改的部分是1和23。

var schemaUri = $display.getItem().getId(); // tcm:1-23-8
var re = /tcm:\d-/gi;  // = 23-8
var schemaIdWithItemType = schemaUri.replace(re, "");
re = /-8/gi;
var schemaId = schemaIdWithItemType.replace(re, "");

If the number is always between the 2 dashes, you could do this: 如果数字始终在2个破折号之间,则可以执行以下操作:

var schemaId = schemaUri.split('-')[1];

This does the following: 这样做如下:

  1. split the string on the '-' character --> ['tcm:1', '23', '8']; split字符串split'-'字符 - > ['tcm:1', '23', '8'];
  2. Get the second item from that array, '23' 从该数组获取第二项, '23'

Or, try this: 或者,试试这个:

var schemaId = schemaUri.match(/-\d+-/)[0].replace(/-/g,'');

This'll find the number in between the dashes with .match(/-\\d+-/) , then remove the dashes. 这将在带有.match(/-\\d+-/)的破折号之间找到数字,然后删除破折号。

Rather than calling $display.getItem().getId(); 而不是调用$display.getItem().getId(); , you can just call $display.getUri(); ,你可以调用$display.getUri(); and then use the split() 然后使用split()

var schemaId = $display.getUri().split('-')[1];

If you did want a pure Regex solution... 如果你确实想要一个纯正的Regex解决方案......

/^tcm:(\d+)-(\d+)(?:-(\d+))?$/i

Should validate your Tridion URI's format and provide you with 3 submatches, the second of which will be the Item ID 应验证您的Tridion URI格式并为您提供3个子匹配,其中第二个将是项目ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM