简体   繁体   English

通过JavaScript获取两个字符串之间的字符串值

[英]Get String value between two strings through javascript

I want to get the string value between ";L0|" 我想获取“; L0 |”之间的字符串值 and ";GTSet" from the following type of strings. 以及以下类型的字符串中的“; GTSet”

var test = "GP0|#9d72d96c-407f-4e45-b2e6-9361faf5808a;L0|#09d72d96c-407f-4e45-b2e6-9361faf5808a|Travel;GTSet|#ac96f075-b7d2-4e90-8dc2-da8875f395fc";
var test2 = "GP0|#15a06b93-f7aa-4dda-b0d6-7bf2d2905f27;L0|#015a06b93-f7aa-4dda-b0d6-7bf2d2905f27|Special Event;GTSet|#ac96f075-b7d2-4e90-8dc2-da8875f395fc";

Here is what i have done already. 这是我已经做过的。

var str = test2.match(";L0|" + "(.*?)" + ";GTSet");
 alert(str[1]);

and this returns a string from the very beginning till the ";GTSet" 从最开始到“; GTSet”都返回一个字符串

Jsfiddle link here jsfiddle链接在这里

I guess you are getting this value from SharePoint Search results, right? 我想您是从SharePoint搜索结果中获得此价值的,对吗? If so, according to Automatically created managed properties in SharePoint Server 2013 : 如果是这样,请根据SharePoint Server 2013中的自动创建的托管属性

Data format for Managed Metadata. 托管元数据的数据格式。

To query for items tagged with a Managed Metadata field, you have to use the Unique Identifier for each label. 要查询带有“托管元数据”字段标记的项目,必须为每个标签使用唯一标识符。 You can find the Unique Identifier for each term in a term set in the Term Store Management Tool, on the GENERAL tab. 您可以在术语库管理工具的“常规”选项卡上的术语集中找到每个术语的唯一标识符。 In addition, the data format that is used in the query has to specify from which level in the term set the query should apply. 此外,查询中使用的数据格式必须指定查询应从术语集的哪个级别应用。 This specification is set by adding one of the following prefixes to the Unique Identifier: 通过向唯一标识符添加以下前缀之一来设置此规范:

  • To query for all items that are tagged with a term: GP0|# 要查询所有带有术语标记的项目: GP0|#
  • To query for all items that are tagged with a child of term: GPP|# 要查询所有用术语子代标记的项目: GPP|#
  • To query for all items that are tagged with a term from a term set: GTSet|# 要从术语集中查询所有带有术语标记的项目,请执行以下操作: GTSet|#

Based on this information the following example demonstrates how to parse search result value for managed metadata : 根据此信息,以下示例演示如何解析托管元数据的搜索结果值

 function parseTaxonomySearchResultValue(val){ var taxValue = {TermSetGuids: [], TermValues: []}; var parts = val.split(';'); parts.forEach(function(part){ if (part.startsWith("GP0|#")) //term? { var termGuid = part.replace("GP0|#", ""); taxValue.TermValues.push({ TermGuid: termGuid}); } else if (part.startsWith("GTSet|#")) //term set? { taxValue.TermSetGuids.push(part.replace("GTSet|#", "")); } else if (part.startsWith("L0|#")) //Term with label? { var termParts = part.replace("L0|#0", "").split('|'); var termGuid = termParts[0]; var termLabel = termParts[1]; var result = taxValue.TermValues.filter(function(tv){ return tv.TermGuid == termGuid; }); if (result.length == 0) taxValue.TermValues.push({TermGuid : termGuid, Label : termLabel}); else result[0].Label = termLabel; } }); return taxValue; } //Usage var taxValue = 'GP0|#9d72d96c-407f-4e45-b2e6-9361faf5808a;L0|#09d72d96c-407f-4e45-b2e6-9361faf5808a|Travel;GTSet|#ac96f075-b7d2-4e90-8dc2-da8875f395fc'; var taxValue = parseTaxonomySearchResultValue(taxValue); document.getElementById('output').innerHTML = "Term info:<br/>" + "Guid= " + taxValue.TermValues[0].TermGuid + "<br/> Label= " + taxValue.TermValues[0].Label; 
 <div id='output'/> 

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

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