var str="<iframe frameborder="0" width="640" height="498" src="https://v.qq.com/iframe/player.html?vid=a0678a3ahqx&tiny=0&auto=0" allowfullscreen=""></iframe><iframe frameborder="0" width="640" height="498" src="https://v.qq.com/iframe/player.html?vid=a0678a3ahqx&tiny=0&auto=0" allowfullscreen=""></iframe>"
var result=str.replace(/(<iframe) (.*) (width=\".*?\") (height=\".*?\") (.*)(<.*?iframe>)/g,"$1 $2 width="100%" $5 $6")
console.log(result)
I want to replace width= "640" height= "498" in iframe with width= "100%"
, so I write the above rule and find that if only one iframe in the string is successfully replaced, but if there are two iframe, it will only replace the second iframe.
ask for help, hope that in each iframe, Width= "640" height= "498" becomes width=
online debugging address http://jsbin.com/dumuxewubu/e.
= split line, problem solved =
found that the previous non-greedy matching method was added wrong, should not use (. *)? Write the question mark in parentheses.
use this
var result=str.replace(/(<iframe) (.*?) (width=\".*?\") (height=\".*?\") (.*?)(<.*?iframe>)/g,"$1 $2 width="100%" $5 $6")