正则表达式搜索子字符串

时间:2015-09-30 19:31:43

标签: regex

我有一个关于如何使用Regexp搜索子字符串的问题。

说,我有一个“基础”字符串,例如:
图像高度= X宽度= Y结束

上述“基”字符串中的高度和宽度都被赋予变化的整数值,即: 图像高度= 15宽度= 20结束
图像高度= 10宽度= 10结束
图像高度= 20宽度= 5结束

所以我的问题是,如果子串的第一部分有效地改变,是否可以使用规范正则表达式获取 宽度值

2 个答案:

答案 0 :(得分:5)

这个正则表达式/width=(\d+)/应该这样做:

var strs = ['image height=15 width=20 end this is another width=123', 'width=200 and image height=10 width=10 end', 'image height=20 width=5 end'];

var widths = strs.map(function(str) {
   var matches = str.match(/\bimage height=\d+ width=(\d+) end\b/);
   return matches && matches.length > 1 ? parseInt(matches[1], 10) : null;
});

document.write(widths);

答案 1 :(得分:0)

如果您不需要您指定的确切表格,Ori Drori的答案是正确的。如果你这样做,你可以使用它:

 $(".avatar-box1").mouseleave(function(){
        $( ".avatar-box1" ).effect( "bounce", "slow" );

    });