我要实现的功能是:用正则表达式,在指定的字符串中寻找中间是o前后是任何字符的3位字符串,将其放在数组中,并将数组的内容输出到页面。var reg = / .o. / g;
var str ="hello world";
var result = new Array();
while(reg.exec(str) != null){
result.push(RegExp.lastMatch);
}
for(var i =0;i<result.length;i++){
document.write(result[i]);
}
为什么出不来结果?
var str ="hello world";
var result = new Array();
while(reg.exec(str) != null){
result.push(RegExp.lastMatch);
}
for(var i =0;i<result.length;i++){
document.write(result[i]);
}
为什么出不来结果?
你的正则里面是不是有空格?去了空格,结果正确输出。
<script type="text/javascript">
var reg = /.o./g;
var str ="hello world";
var result = new Array();
while(reg.exec(str) != null){
result.push(RegExp.lastMatch);
}
document.write(result.join("_"));
</script>
去除空间是正常了