Replace (/ "([^"] *) "/ g," $1' ") has the same effect as replace (/" / g, "'"). Why use the former?

in the introduction to the replace () method of w3school, there is an example:

// 
name = ""a", "b"";
name.replace(/"([^"]*)"/g, ""$1""); // "a", "b"

and replace it with the following abbreviation

name = ""a", "b"";
name.replace(/"/g, """); // "a", "b"

the effect is the same. If the latter can be realized, why use the former? Is it just to illustrate the use of $1 or is it beneficial?


these two regular expressions have different meanings
you replace (/ "/ g,"'"); converts all " into '

.

and the expression replace (/ "([^"] *) "/ g,"'$1' "); wraps all double quotation marks without the value of double quotation marks in the middle, replacing it with single quotation marks.

for example, there is a string like this: '"dsds" dsd "," ggfgfg "
the replacement result is: `

'"dsds"dsd","ggfgfg"'.replace(/"/g, "'");//:'dsds'dsd','ggfgfg'
'"dsds"dsd","ggfgfg"'.replace(/"([^"]*)"/g, "'$1'"); //: 'dsds'dsd','ggfgfg"
Menu