例如,正则表达式/bed(?=room)/只能匹配子串"bedroom",而子串"room"并不作为匹配结果返回,以进行后续处理。同样正则表达式/Windows(?=2000|xp)/不能匹配WindowsNT,只能匹配其后紧跟子串"2000"或"xp"的子串Windows,而子串"2000"和"xp"并不作为结果返回。
实例:
<script type="text/javascript">
<!--
var str = "bedroom";
var reg = /^bed(?=room$)/g;
var arr = reg.exec(str);
var msg;
if(arr){
}else{
msg = "匹配失败<br>";
}
document.write(msg);
-->
</script>
输出:
bed成功匹配
---------------------------------------------------------------------------------------------------------------
var str = "Windows2000 Windowsxp";
var reg = /Windows(?=2000|xp)/g;
arr = reg.exec(str);
Windows成功匹配
Windows成功匹配 原文链接:https://www.f2er.com/regex/359016.html