简体   繁体   English

正则表达式字符串JS

[英]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).我只需要从这个字符串中获取范围内的这个数字 - 180(这里可以是不同的数字,并且字符串可以有其他 gext,但始终应该从范围中获取数字)。

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.我看到了一些例子,但结果总是没有我想要的。

Thanks谢谢

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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM