开发者

Extracting specific text from select option boxes

It's possible I'm just misusing .filter() he开发者_如何学编程re, but I've been trying to extract a particular piece of text from javascript option boxes without much success.

In short, each of my option boxes has a bunch of info, then, in brackets, ([x] items left in stock).

I'd like to extract that [x], but I can't seem to get any sort of regular expression filtering working in jQuery - or, perhaps, I'm doing it wrong.

In short, if anyone could complete the below, I'd be very appreciative:

$('.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]
});

Any help very appreciated.


Something along these lines:

$('.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];
});

Of course, this expression depends on the fact that your data is formatted as (number) string


This should get you what you need.

var pattern=/\[(\d+)\] items left in stock/;
var qty = pattern.exec(selectedItem)[1];


So you should use the JavaScript string.match function

var s = "[100] items left in stock";

var p = s.match(regexp);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜