我有一个配置文件,如
[其他配置内容]
aa=xxxx
bb=cccc
cc=dddd
ee=ffff
[以换行分割]
aa=11
bb=22
cc=33
ee=44
[其他配置内容]我需要用正则表达式检测出两个区域
我现在用了两个表达式来测试
virtual=(.+)(?=\r?\n\s*\r?\n)
^virtual=([\s\S]*)
但结果都不对,请教达人
//----------------------------------------------以下是我的测试代码
<script type="text/javascript">
function emptyRow(){
var txts=document.getElementById("texts").value;
document.getElementById("result").value=txts.match(/virtual=(.+)(?=\r?\n\s*\r?\n)/g);
//cr=;
//alert(cr);
}
</script>
<textarea id="texts" style="width:100%;height:150px;">
virtual=192.168.10.251:8010           虚拟地址
real=192.168.10.217:8010 gate    真实服务器地址 gate是转发方法,其它还有masq ipip 
real=192.168.10.218:8010 gate
fallback=127.0.0.1:8010 gate     失败返回地址
service=http            服务协议:有http,ftp,smtp,pop,ldap,mysql,orcale
request="/bin"       验证地址
receive="xml"        验证内容
scheduler=wrr         调度方式:有wrr,rr,wlc
protocol=tcp           协议:有tcp,udp
checktype=negotiate   检查类型
virtualhost= www.x.y.zvirtual=192.168.10.251:8010           虚拟地址
real=192.168.10.217:8010 gate    真实服务器地址 gate是转发方法,其它还有masq ipip 
real=192.168.10.218:8010 gate
fallback=127.0.0.1:8010 gate     失败返回地址
service=http            服务协议:有http,ftp,smtp,pop,ldap,mysql,orcale
request="/bin"       验证地址
receive="xml"        验证内容
scheduler=wrr         调度方式:有wrr,rr,wlc
protocol=tcp           协议:有tcp,udp
checktype=negotiate   检查类型
virtualhost= www.x.y.z
</textarea>
<textarea id="result" style="width:100%;height:150px;"></textarea>
<input type="button" value="Test" onclick="emptyRow();"/>

解决方案 »

  1.   

    jnkc,这个只可以匹配到一行的内容呀!
      

  2.   

    [其他配置内容]
    aa=xxxx
    bb=cccc
    cc=dddd
    ee=ffff
    [以换行分割]
    aa=11
    bb=22
    cc=33
    ee=44
    [其他配置内容]我需要匹配出
    aa=xxxx
    bb=cccc
    cc=dddd
    ee=ffff

    aa=11
    bb=22
    cc=33
    ee=44
      

  3.   

    document.getElementById("result").value=txts.match(/virtual=(.+)(?=\r?\n\s*)/g);
      

  4.   

    or
    document.getElementById("result").value=txts.match(/real=(.+)(?:\r?\n\s*)/g).join("");