JavaScript:正则表达式 应用
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);
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=0;i<data.length;i++) { var s = data[i]; str += "源字符串:" + s + "\n"; if(reg.test(s)) { str+= "name: " + RegExp.$2 + "\n"; str += "src: " + RegExp.$4 + "\n"; } } console.log(str);
3.