gpt4 book ai didi

javascript - 似乎无法为特定情况制作正则表达式

转载 作者:行者123 更新时间:2023-12-03 11:46:24 26 4
gpt4 key购买 nike

我正在使用 Node ,并且需要将 UNIX 类型的 grep 命令应用于 HTTP GET 之后收到的正文响应。这是传入的文本(正文变量):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <om2m:discovery xmlns:om2m="http://uri.etsi.org/m2m" xmlns:xmime="http://www.w3.org/2005/05/xmlmime">
<om2m:matchSize>65</om2m:matchSize>
<om2m:discoveryURI>
<reference>gscl</reference>
<reference>gscl/scls</reference>
<reference>gscl/scls/subscriptions</reference>
<reference>gscl/scls/mgmtObjs</reference>
<reference>gscl/scls/mgmtObjs/subscriptions</reference>
<reference>gscl/applications</reference>
<reference>gscl/applications/subscriptions</reference>
<reference>gscl/applications/mgmtObjs</reference>
<reference>gscl/applications/mgmtObjs/subscriptions</reference>
<reference>gscl/accessRights</reference>
<reference>gscl/accessRights/subscriptions</reference>
<reference>gscl/groups</reference>
<reference>gscl/groups/subscriptions</reference>
<reference>gscl/containers</reference>
<reference>gscl/containers/subscriptions</reference>
<reference>gscl/subscriptions</reference>
<reference>gscl/accessRights/AR_ADMIN</reference>
<reference>gscl/accessRights/AR_ADMIN/subscriptions</reference>
<reference>gscl/scls/nscl</reference>
<reference>gscl/scls/nscl/applications</reference>
<reference>gscl/scls/nscl/applications/subscriptions</reference>
<reference>gscl/scls/nscl/applications/mgmtObjs</reference>
<reference>gscl/scls/nscl/applications/mgmtObjs/subscriptions</reference>
<reference>gscl/scls/nscl/accessRights</reference>
<reference>gscl/scls/nscl/accessRights/subscriptions</reference>
<reference>gscl/scls/nscl/groups</reference>
<reference>gscl/scls/nscl/groups/subscriptions</reference>
<reference>gscl/scls/nscl/containers</reference>
<reference>gscl/scls/nscl/containers/subscriptions</reference>
<reference>gscl/scls/nscl/subscriptions</reference>
<reference>gscl/scls/nscl/attachedDevices</reference>
<reference>gscl/scls/nscl/attachedDevices/subscriptions</reference>
<reference>gscl/scls/nscl/notificationChannels</reference>
<reference>gscl/scls/nscl/mgmtObjs</reference>
<reference>gscl/scls/nscl/mgmtObjs/subscriptions</reference>
<reference>gscl/scls/nscl/m2mPocs</reference>
<reference>gscl/applications/98191axla</reference>
<reference>gscl/applications/98191axla/subscriptions</reference>
<reference>gscl/applications/98191axla/containers</reference>
<reference>gscl/applications/98191axla/containers/subscriptions</reference>
<reference>gscl/applications/98191axla/accessRights</reference>
<reference>gscl/applications/98191axla/accessRights/subscriptions</reference>
<reference>gscl/applications/98191axla/groups</reference>
<reference>gscl/applications/98191axla/groups/subscriptions</reference>
<reference>gscl/applications/98191axla/notificationChannels</reference>
<reference>gscl/applications/98191axla/containers/SENSOR </reference>
<reference>gscl/applications/98191axla/containers/SENSOR/contentInstances</reference>
<reference>gscl/applications/98191axla/containers/SENSOR/contentInstances/subscriptions</reference>
<reference>gscl/applications/98191axla/containers/SENSOR/subscriptions</reference>
<reference>gscl/applications/98191axla/containers/SENSOR/contentInstances/CI_23326802</reference>
<reference>gscl/applications/98191axla/containers/DATA</reference>
<reference>gscl/applications/98191axla/containers/DATA/contentInstances</reference>
<reference>gscl/applications/98191axla/containers/DATA/contentInstances/subscriptions</reference>
<reference>gscl/applications/98191axla/containers/DATA/subscriptions</reference>
<reference>gscl/applications/98191axla/containers/DATA/contentInstances/CI_566628946</reference>
<reference>gscl/applications/98191axla/containers/CONT_81747241</reference>
<reference>gscl/applications/98191axla/containers/CONT_81747241/contentInstances</reference>
<reference>gscl/applications/98191axla/containers/CONT_81747241/contentInstances/subscriptions</reference>
<reference>gscl/applications/98191axla/containers/CONT_81747241/subscriptions</reference>
<reference>gscl/applications/98191axla/containers/SENSOR/contentInstances/CI_285448423</reference>
<reference>gscl/applications/98191axla/containers/CONT_360523761</reference>
<reference>gscl/applications/98191axla/containers/CONT_360523761/contentInstances</reference>
<reference>gscl/applications/98191axla/containers/CONT_360523761/contentInstances/subscriptions</reference>
<reference>gscl/applications/98191axla/containers/CONT_360523761/subscriptions</reference>
<reference>gscl/applications/98191axla/containers/DATA/contentInstances/CI_395775165</reference>
</om2m:discoveryURI> </om2m:discovery>

从该文本中,我需要提取应用程序容器之间的ID,在本例中等于98191axla。我尝试使用 match 方法来做到这一点:

var searchFor="\s<reference>gscl\/applications\.\d\/DATA\/contentInstances\/subscriptions<\/reference>";
console.log("Looking for:"); console.log(searchFor);
var found = body.match(searchFor);

总是返回-1。我以前从未使用过正则表达式,所以我不得不尝试不同的东西......它们都不起作用。任何帮助深表感谢!

谢谢!(顺便说一句,我知道它是一种 xml 表示形式,但它并不是一个具有非常不同子级的复杂表示形式,因此我认为操作文本会更容易......)

最佳答案

您可以使用此代码:

var re = /<reference>gscl\/applications\/(\w+)\/containers\/DATA\/congtentInstances\/subscriptions<\/reference>/;
var m = str.match(re);
if (m) console.log(m[1]);

请注意,正则表达式对象是在斜杠之间声明的(不将模式括在引号之间)。

该模式使用\w character class包含 ascii 数字和字母(以及下划线)。该类后面是 quantifier + 表示一个或多个。最后,为了隔离您需要的子字符串, capture group已使用。

关于javascript - 似乎无法为特定情况制作正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26028284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com