Regex troubles with a variable

Jesse Chung
Apr 27, 2022

--

var t = 'some sting characters ktestvarj';

var variables = {
'testvar': 123,
'testvar1': 456
};

function replacer(match, p1, offset, string) {
return variables[p1];
}

var modify = function (str) {
var teststr = str.replace(/k(.*?)j/g, replacer);
console.log(teststr);
}

modify(t);
modify('some ktestvarj sting characters ktestvarj ktestvar1j');

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Jesse Chung
Jesse Chung

Written by Jesse Chung

0 Followers

Software Engineer of 3 years

No responses yet

Write a response