jason jason - 1 year ago 130
Javascript Question

Regexp string JS

didn't work with regexp previously, and now I need to get some part of string.

var str1 = "Wi-Fi: 6.3 ounces (180 grams); Wi-Fi + 3G: 6.6 oz (188 g)Actual size and weight may vary by configuration and manufacturing process";

I need get from this string only this digital from scope - 180 (here can be diferent digitals, and string can have other gext, but always should take digital from the scopes).

Here's my code:

var weight = $(data, "body").find("td:contains('Weight')").siblings().text();
var regexp = /\b(\d*\.?\d?)\sounces/im;
var found = weight.replace(regexp, "");
if (weight && weight != "") {
return found;

How to realize it? I saw some examples, but all the time in result have not what I want.


Answer Source

Resolved, work with next code:

var weight = $(data, "body").find("td:contains('Weight')").siblings().text();

var regexp =  /\b(\d*\.?\d?)\sounces/im;
var found = weight.match(regexp);
if (weight && weight != "") {
    return found[0];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download