javascript - JS: how to find a specific string, then extract an integer? -
i'm trying write userscript game i'm playing. uses piece of html code:
<td valign="center"> <b>ten-leaf clover</b> (4 left in stock today) </td>
this picture of we're talking about:
the script should search string containing words "left in stock today", integer within string. (the '4' not constant, changes every day.) lastly, store integer variable, can replace '1' in input field. this:
var clover = entercodehere $("input.text[name='quantity']").val(clover);
you can used regex so:
var texttosearch = $("td").innerhtml(); //you'll need better selector this. better use class or id var clover = parseint(texttosearch.match(/\d+\s*left in stock today/)[0].match(/\d+/)[0]); $("input.text[name='quantity']").val(clover);
you may want check array isn't empty before taking first value if confident it'll there should grand.
Comments
Post a Comment