如何解决从选择选项框中提取特定文本
| 我可能在这里只是误用了.filter(),但是我一直在尝试从javascript选项框中提取特定文本,但收效甚微。 简而言之,我的每个选项框都有一堆信息,然后放在方括号中(库存中剩余[x]件)。 我想提取那个[x],但是我似乎无法在jQuery中使用任何形式的正则表达式过滤-也许我做错了。 简而言之,如果有人可以完成以下工作,我将不胜感激:$(\'.productSelect\').change(function() {
selectedItem = $(this + \'option:selected\').text();
# Now grab the integer \"x\" inside the ([x] items left in stock) part of the text
[ your code here]
});
任何帮助非常感谢。
解决方法
遵循以下原则:
$(\'.productSelect\').change(function() {
selectedItem = $(this + \'option:selected\').text();
# Now grab the integer \"x\" inside the ([x] items left in stock) part of the text
var n = selectedItem.match(/(\\()(\\d+)(\\))/)[2];
});
当然,此表达式取决于您的数据格式为“ 2”的事实
,这应该为您提供所需的东西。
var pattern=/\\[(\\d+)\\] items left in stock/;
var qty = pattern.exec(selectedItem)[1];
,因此,您应该使用JavaScript string.match函数
var s = \"[100] items left in stock\";
var p = s.match(regexp);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。