Home > Software engineering >  Javascript - why regular expression replace not working for loops?
Javascript - why regular expression replace not working for loops?

Time:08-19

I'm trying to replace following patterns using regular expressions.

function get_table_name(table_partition_name) {
  var result = table_partition_name;
  const atQueryRegex = "[@].*"
  result = result.replace(atQueryRegex, "")

  const uuid64Regex   = /_?[a-f0-9]{64}/i;
  const uuid32Regex   = /_?[a-f0-9]{8}(-|_)?[a-f0-9]{4}(-|_)?[a-f0-9]{4}(-|_)?[a-f0-9]{4}(-|_)?[a-f0-9]{12}/i;
  const uuid24Regex   = /_?[a-f0-9]{24}/i;
  const mmyyyyRegex   = /_?[0-9]{2}__mm_[0-9]{2}_yyyy_[0-9]{4}/i;
  const flowridaRegex = /_?5000y00001[a-z0-9]{8}/i;
  const timeRegex     = /_?[0-9]{4,8}t?[0-9_]*/i;
  const numRegex      = /_?[0-9] [0-9_]*(_|$)/i;
  const tsReplacement = "_*";
  const tsRegexes = [
      uuid64Regex,
      uuid32Regex,
      uuid24Regex,
      mmyyyyRegex,
      flowridaRegex,
      timeRegex,
      numRegex
  ];
  for (const expr of tsRegexes) {
      result = result.replace(expr, tsReplacement);
      console.log(expr);
      console.log(result);
  }
  
  return result
}

test = "DWEB_UI_ROLLOUT__31259_f3e3ec7e0f54b96d0d26c7fe_snap_direct_snap_replay_view_event_user_bcce6842c5acde4a0d0b232f_6_7_cumulative_20220802_20220808"
console.log(get_table_name(test)) 

However, the final result is DWEB_UI_ROLLOUT__**_snap_direct_snap_replay_view_event_user_bcce6842c5acde4a0d0b232f_*cumulative_20220802_20220808; while DWEB_UI_ROLLOUT__**_snap_direct_snap_replay_view_event_user_*_*cumulative_* is expected.

When looking thru all the printed output, some regular expressions are not functioning as expected, as marked in the red box. However, when I tried that specific expression it does work.

enter image description here

Here is how the single test would work:

test = "DWEB_UI_ROLLOUT__**_snap_direct_snap_replay_view_event_user_bcce6842c5acde4a0d0b232f_*cumulative_20220802_20220808"

console.log(test.replace(/_?[a-f0-9]{24}/i, "_*"))

Result:

DWEB_UI_ROLLOUT__**_snap_direct_snap_replay_view_event_user_*_*cumulative_20220802_20220808

CodePudding user response:

uuid24Regex matches two times. At first for _f3e3ec7e0f54b96d0d26c7fe and then again for _bcce6842c5acde4a0d0b232f. The third iteration of the loop (processing uuid24Regex) only replaces the first match which you used in your test. By testing manually (outside of the loop) you replaced the second match.

In order to replace both occurrences within the loop you have to use String.prototype.replaceAll(). The regular expressions then must have the global flag (g) set. (e.g. /_?[a-f0-9]{64}/gi).

The following code snippet results in the string you expect:

DWEB_UI_ROLLOUT__**_snap_direct_snap_replay_view_event_user_*_*cumulative_*
function get_table_name(table_partition_name) {
  var result = table_partition_name;
  const atQueryRegex = "[@].*"
  result = result.replace(atQueryRegex, "")

  const uuid64Regex   = /_?[a-f0-9]{64}/gi;
  const uuid32Regex   = /_?[a-f0-9]{8}(-|_)?[a-f0-9]{4}(-|_)?[a-f0-9]{4}(-|_)?[a-f0-9]{4}(-|_)?[a-f0-9]{12}/gi;
  const uuid24Regex   = /_?[a-f0-9]{24}/gi;
  const mmyyyyRegex   = /_?[0-9]{2}__mm_[0-9]{2}_yyyy_[0-9]{4}/gi;
  const flowridaRegex = /_?5000y00001[a-z0-9]{8}/gi;
  const timeRegex     = /_?[0-9]{4,8}t?[0-9_]*/gi;
  const numRegex      = /_?[0-9] [0-9_]*(_|$)/gi;
  const tsReplacement = "_*";
  const tsRegexes = [
      uuid64Regex,
      uuid32Regex,
      uuid24Regex,
      mmyyyyRegex,
      flowridaRegex,
      timeRegex,
      numRegex
  ];
  for (const expr of tsRegexes) {
      result = result.replaceAll(expr, tsReplacement);
      console.log(expr);
      console.log(result);
  }
  
  return result
}

test = "DWEB_UI_ROLLOUT__31259_f3e3ec7e0f54b96d0d26c7fe_snap_direct_snap_replay_view_event_user_bcce6842c5acde4a0d0b232f_6_7_cumulative_20220802_20220808"
console.log(get_table_name(test)) 
  • Related