Javascript搜索字符串和显示值

Javascript搜索字符串和显示值,javascript,reactjs,typescript,for-loop,Javascript,Reactjs,Typescript,For Loop,我有一个脚本项目。在我的result数组中,我有一个返回url的值。url包含/Items(142)/。我想检索项目ID并将其存储在变量中/ 我知道我可以做一些类似于result.data.odata.editLink.search()的事情,但是里面的数字总是在变化。我该怎么做呢 这是我目前拥有的 for (let file of this.state.requestFiles) { r.item.attachmentFiles.add(file.name, file).then(re

我有一个脚本项目。在我的
result
数组中,我有一个返回url的值。url包含
/Items(142)/
。我想检索项目ID并将其存储在变量中/

我知道我可以做一些类似于
result.data.odata.editLink.search()
的事情,但是里面的数字总是在变化。我该怎么做呢

这是我目前拥有的

for (let file of this.state.requestFiles) {
    r.item.attachmentFiles.add(file.name, file).then(result => {
        console.log(result);
        console.log(result.data.odata.editLink);

        let requestID = //Here is where I want to put the ID fround in the url 
    })
}
以下是我的结果控制台:

{data: {…}, file: AttachmentFile}
data:
FileName: "Capture-problem.PNG"
FileNameAsPath: {DecodedUrl: "Capture-problem.PNG"}
ServerRelativePath: {DecodedUrl: "/sites/Test/Attachments/142/Capture-problem.PNG"}
ServerRelativeUrl: "/sites/TestAttachments/142/Capture-problem.PNG"
odata.editLink: "Web/Lists(guid'24b9fe8b-3b87-44e4-b185-706d543d9567')/Items(142)/AttachmentFiles('Capture-problem.PNG')"
odata.type: "SP.Attachment"

odata.editLink
()
中具有ID。为此,可以在正则表达式中使用命名捕获组:

const re=/Items\(?\d+)/
const str=“Web/list(guid'24b9fe8b-3b87-44e4-b185-706d543d9567')/Items(142)/AttachmentFiles('Capture-problem.PNG')”
const matches=re.exec(str)

console.log(matches.groups.id)
您始终可以使用正则表达式:

var re = /(?:Items\()(\d+)\)/g; 
var string = "odata.editLink: \"Web/Lists(guid'24b9fe8b-3b87-44e4-b185-706d543d9567')/Items(142)/AttachmentFiles('Capture-problem.PNG')\"";
var found = re.exec(string);
var itemsVal = found[1];
console.log(itemsVal); 

将输出
142

,因此我添加了您的代码和
匹配项。id
给了我一个错误,它说
属性“id”在类型“RegExpExecArray”上不存在
,很抱歉,我键入得太快了。查看
match.groups.id
(我编辑了我的答案来解决这个问题)谢谢。看来我也犯了同样的错误。现在突出显示
。但是犯同样的错误,这很奇怪。我把它变成了一个可运行的代码段,它肯定对我有用。真奇怪,它说我
错误TS2339:属性“groups”在类型“RegExpExecArray”上不存在
这是一个
字符串
,但是它会动态变化,所以我应该如何使用
result.data.odata.editLink
而不是只传递整串:-)