I want to replace a string contain arr_key[key] by arr_value[key] with RegExp
method. In this case 1<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>
by <ruby>1日<rp>(</rp><rt>ついたち</rt><rp>)</rp></ruby>
etc..
I try with RegExp
: /1<ruby>日<rp>\(<\/rp><rt>にち<\/rt><rp>\)<\/rp><\/ruby>/g
but it works not exactly. It also replaces something like 2<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>
var my_string ="1<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>abc";
function repair_input(rec){
var arr_key ={
key1:"1<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>",
key2:"2<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>",
key3:"3<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>",
};
var arr_value={
key1:"<ruby>1日<rp>(</rp><rt>ついたち</rt><rp>)</rp></ruby>",
key2:"<ruby>2日<rp>(</rp><rt>ふつか</rt><rp>)</rp></ruby>",
key3:"<ruby>3日<rp>(</rp><rt>みっか</rt><rp>)</rp></ruby>",
}
for (var key in arr_key) {
var my_regex = arr_key[key];
var my_value = arr_value[key];
my_regex=my_regex.replace(/\//g,"\\/");
my_regex=my_regex.replace(/\(/g,"\\(");
my_regex=my_regex.replace(/\)/g,"\\)");
my_regex = "/"+my_regex+"/g";
console.log(my_regex);
var rec =rec.replace(my_regex,my_value);
}
return rec;
};
This code doesn't work at step var rec =rec.replace(my_regex,my_value);
.
But when I past result which are received from step console.log(my_regex);
for my_regex in step var rec =rec.replace(my_regex,my_value);
. It works not exactly. Thank you for your helping.