gpt4 book ai didi

javascript - 如何获取方括号中的内容

转载 作者:行者123 更新时间:2023-11-30 17:47:40 25 4
gpt4 key购买 nike

如何获取值在方括号中为真。我无法通过从方括号中获取值来获取选项是真还是假。

例如我有ID:

values = content.match(/[^[\]]+(?=])/g);
$(this).closest(".myclass").myFn({
postMe: values[0],
});

value[0] 是从 ID (#myID) 获取的第一个值,但它不会运行。我把方括号放在:

<div id="myID">[true]</div>

但我直接说:

values = content.match(/[^[\]]+(?=])/g);
$(this).closest(".myclass").myFn({
postMe: true,
});

运行正常。说说这个问题的解决方案。感谢您的帮助。

最佳答案

您可以使用 test而不是 match这里像:

var result = content.test(/[^[\]]+(?=])/g);
$(this).closest(".myclass").myFn({
postMe: result,
});

test 执行正则表达式和指定字符串之间的匹配项搜索,然后返回 truefalse 基于它。

match 仅在将字符串与 正则表达式 匹配时检索匹配项(以数组的形式)。

关于javascript - 如何获取方括号中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19788369/

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