I need to replace two strings using regular expression value replacement so the resulting string is $?tlang=es&text=Hello world, so I didn't know to use here String.prototype.replace().
const value = "Hello world"
const queryString = "?tlang=es&text=$1"
In this scenary, value and queryString are hard-coded, but in "real life" it should be the result of a regular expression group capturing like line.match(/msgid \"(.*)\"/) where line is an iterated text line and queryString is what the user submitted.
I thought I just could do this, but maybe it's too much effort where there is a better solution (that I couldn't find):
const line = "Full name: John Doe" // text input
const sourcePattern = /Full name: (.*) (.*)/ // user input
let queryString = 'name=$1&lname=$2' // user input
const matches = line.match(sourcePattern)
matches.splice(0, 1)
for (let i = 0; i < matches.length; i++) {
queryString = queryString.replace(`\$${i+1}`, matches[i])
}
Any ideas?