0

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.

Co.UQ
  • 29
  • 6
  • 1
    does your `my_string` have `abc` at end or it is typed by mistake ? – Code Maniac Jul 21 '19 at 05:04
  • Using a regular expression sounds like a bad plan. Why can't you just parse it as HTML and use selectors? – tadman Jul 21 '19 at 05:11
  • my_string is input and unpredictable. arr_key[key] can appear one or more times at anywhere. And I wish to see output which will be replaced corresponding with arr_value[key]. – Co.UQ Jul 21 '19 at 07:18
  • @tadman Would you like to explain more by code? – Co.UQ Jul 21 '19 at 07:44

1 Answers1

0

It's better to use a single object for mapping key/values instead of using two different object, and then read value based on key from that object

var my_string = "1<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>abc";

function repair_input(rec) {
  rec = rec.match(/^.*<\/ruby>/i)
  rec = rec ? rec[0] : rec
  var repairObj = {
    "1<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>":"<ruby>1日<rp>(</rp><rt>ついたち</rt><rp>)</rp></ruby>",
    "2<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>":"<ruby>2日<rp>(</rp><rt>ふつか</rt><rp>)</rp></ruby>",
    "3<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>":"<ruby>3日<rp>(</rp><rt>みっか</rt><rp>)</rp></ruby>",

  };
  return repairObj[rec] ? repairObj[rec] : 'No matching value found'
};

console.log(repair_input(my_string))
console.log(repair_input('some random string'))

Note:- Not sure if abc at end of my_string is part of your input or not, if it is not then you can exclude this part

  rec = rec.match(/^.*<\/ruby>/i)
  rec = rec ? rec[0] : rec

Use regex only when you can't do it with HTML parser or any such sort of standard thing which is meant for such tasks


Update:- ( and ) are special character in regex, so you need to escape it to match, else it will be interpreted as capturing group

var my_string = "1<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>abc";

function repair_input(rec) {
  var repairObj = {
    "1<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>": "<ruby>1日<rp>(</rp><rt>ついたち</rt><rp>)</rp></ruby>",
    "2<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>": "<ruby>2日<rp>(</rp><rt>ふつか</rt><rp>)</rp></ruby>",
    "3<ruby>日<rp>(</rp><rt>にち</rt><rp>)</rp></ruby>": "<ruby>3日<rp>(</rp><rt>みっか</rt><rp>)</rp></ruby>",

  };
  Object.keys(repairObj).forEach(reg=>{
    temp = reg.replace(/([)(])/g, '\\$1')
    let regex = new RegExp(temp, 'gi')
    rec = rec.replace(regex, repairObj[reg])
  })
  return rec
};

console.log(repair_input(my_string))
Code Maniac
  • 37,143
  • 5
  • 39
  • 60