Regular, the question mark is still greedy to match the problem.

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")

solved, found that the previous non-greedy matching method 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")

if there are no performance requirements (but it must be much faster than using jquery to insert and modify), you can use cheerio, so you don't have to mess with regularities (and it's safe)

import cheerio from 'cheerio';
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 $ = cheerio.load(str, {decodeEntities: false});
$('iframe').attr('width','100%')
console.log($.html())
Menu