B
B
Banan442020-05-01 19:42:31
JavaScript
Banan44, 2020-05-01 19:42:31

Problem with brackets in regular expressions, what's wrong?

const object = {
    Component: 'Component Yes',
    Composer: 'Composer Yes'
}

let string = "{{Component}} {{Composer}}"
string = string.replace(/\{\{(.*?)\}\}/, object[$1])

// Undefined undefined

// Я хочу достать то что выполнилось в скобках, разве для этого  $1 не нужен?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2020-05-01
@Banan44

string = string.replace(/{{(.*?)}}/g, (m,m1) => object[m1])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question