I have a string which contains characters that should be replaced once (at first appearance).
These characters are:
- L => will be replaced with the lecture name
- N => will be replaced with a name
- D => will be replaced with a date
Example input:
L_N_L_D
Desired result (note that only the first L is replaced):
Math_Ex01_L_2018-10-05
My current code (simplified for the example):
let res = file_string.replace(/L|N|D/, x => {
switch (x) {
case 'L': return lecture;
case 'N': return name;
case 'D': return date;
default: return x;
}
});
What I get is this:
Math_L_N_D
If I change the regex to /L|N|D/g the second L will also be replaced which is not what I want.
How could this be implemented?
let res = file_string.replace('D', date).replace('N', name).replace('L', lecture)- note the order