Can someone explain me why the the following 2 the same Regular Expressions return different results when you run it "static" or dynamical? Should I add some escape character?
let arr = []
let re = /(\w+)\s(\w+)/
let str = 'John Smith'
let newstr = str.replace(re, '$2, $1')
arr.push(newstr)
let code=`
let re = /(\w+)\s(\w+)/
let str = 'John Smith'
let newstr = str.replace(re, '$2, $1')
newstr
`
arr.push(eval(code))
arr
Output:
[
"Smith, John",
"John Smith"
]