JavaScript:正则表达式 应用

时间:2021-03-15 07:36:23

1.

  var data = "<table id=\"test\"><tr class=\"light\"><td> test </td></tr></table>";
var reg = /<([a-z]+)[^>]*>/ig;
var a = data.replace(reg, "<$1>");
console.log(a);

JavaScript:正则表达式 应用

2.

var str="";
var data = [' <img alt="" border="0" name="g6-o44-1" onload="DrawImage" src="/bmp/foo1.jpg" />', ' <img src="/bmp/foo2.jpg" alt="" border="0" name="g6-o44-2" onload="DrawImage" />'] ;
var reg = /<img\b(?=(?:(?!name=).)*name=(['"]?)([^'"\s>]+)\1)(?:(?!src=).)*src=(['"]?)([^'"\s>]+)\3[^>]*>/i;
for(var i=;i<data.length;i++)
{
var s = data[i];
str += "源字符串:" + s + "\n"; if(reg.test(s))
{
str+= "name: " + RegExp.$ + "\n";
str += "src: " + RegExp.$ + "\n";
}
}
console.log(str);

JavaScript:正则表达式 应用

3.