let a = `<script crossOrigin="anonymous" src="//example.com/index.js"></script>`
let regex = new RegExp(
`<script(.*?) src="` + '//example.com/index.js' + `"></script>`, 'g')
let replacementString = 'document.cookie=e.replace(/[^+#$&^`|]/g,encodeURIComponent).replace("(","%28").replace(")","%29")+"="+t.replace(/[^+#$&\/:<-\[\]-}]/g,encodeURIComponent)+(r.domain?";domain="+r.domain:"")+(r.path?";path="+r.path:"")+(r.secure?";secure":"")+(r.httponly?";HttpOnly":"")'
b = a.replace(regex, replacementString)
The b
result is:
document.cookie=e.replace(/[^+#<script crossOrigin="anonymous" src="//example.com/index.js"></script>^`|]/g,encodeURIComponent).replace("(","%28").replace(")","%29")+"="+t.replace(/[^+#<script crossOrigin="anonymous" src="//example.com/index.js"></script>/:<-[]-}]/g,encodeURIComponent)+(r.domain?";domain="+r.domain:"")+(r.path?";path="+r.path:"")+(r.secure?";secure":"")+(r.httponly?";HttpOnly":"")
while the expected result is the replacementString
value as is.
Why did the outcome turn out differently?