如何获取值在方括号中为真。 我无法通过从方括号中获取值来获取选项是真还是假。
例如我有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,
});
运行正常。说说这个问题的解决方案。感谢您的帮助。
最佳答案
var result = content.test(/[^[\]]+(?=])/g);
$(this).closest(".myclass").myFn({
postMe: result,
});
当 test
执行正则表达式和指定字符串之间的匹配项搜索,然后返回 true 或 false 基于它。
而 match
仅在将字符串与 正则表达式 匹配时检索匹配项(以数组的形式)。
关于javascript - 如何获取方括号中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19788369/