js 正則,根據css id名class名取得一個屬性程式碼;
阿新 • • 發佈:2018-12-25
var oldLine=all.match( new RegExp('.cp_hover'+'(\\s)*'+'.hot3'+'(.)*'+'}','g') );
alert(oldLine);
1:以小括號區分一塊塊
2:\\s代表空格鍵,轉義後\s,s是space
3:‘g’,作用全域性,global
<span style="white-space:pre"> </span>/*作用:根據class屬性取得css一個屬性程式碼 <span style="white-space:pre"> </span>引數0:所有css程式碼 <span style="white-space:pre"> </span>引數1:class1或id <span style="white-space:pre"> </span>引數2:class2或id <span style="white-space:pre"> </span>. <span style="white-space:pre"> </span>. <span style="white-space:pre"> </span>引數n:classn <span style="white-space:pre"> </span>*/ <span style="white-space:pre"> </span>var getOcssByClassName=function(){ <span style="white-space:pre"> </span>//根據引數取生成正則表示式 <span style="white-space:pre"> </span>var par=""; <span style="white-space:pre"> </span>for(var i=1;i<arguments.length;i++){<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>par=par+'(\\s)*'+arguments[i]; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>par=par+'(\\s)*'+'{'+'(.)*'+'}'; <span style="white-space:pre"> </span>//取得一行屬性 <span style="white-space:pre"> </span>//var oldLine=arguments[0].match( new RegExp('.cp_hover'+'(\\s)*'+'.hot3'+'(\\s)*'+'{'+'(.)*'+'}','gmi') ); <span style="white-space:pre"> </span>var oldLine=arguments[0].match( new RegExp(par,'gmi') ); <span style="white-space:pre"> </span>return oldLine; <span style="white-space:pre"> </span>}