Search code examples
javascriptregexgoogle-chrome-devtools

I've tried to get this regex replacement to work in the chrome snippets replacement panel `/((m|n))0/ ` but couldn't figure it out


Here's the snippet:

//Get userId,questionId and title 
  let btn0 = document.createElement('input');
  btn0.type = "button";
  btn0.value = 'Read Content';
  btn0.onclick = () => {
  console.clear();
  let b = [["Item","String"]];
  let c = document.querySelectorAll('div#un1')[0].children;
  let a = Array.from(c);
  a.forEach((e , i) => {
    b.push([i + 1,e.innerText]);
  });
  localStorage.setItem("test",JSON.stringify(b));  
  console.log(JSON.stringify(b)); 
  }
  if(m0)m0.prepend(btn0);

I was just trying to replace btn0 to btn1 and m0 to m1 to start another snippet without rewriting all of common stuff. I ended up using something I wrote but would like to be able to use the builtin tool in Chrome dev tools.

I was hoping one of you knows how to use the replacement tool on Chrome Dev Tools better than I.


Solution

  • It seems the regex engine used in Chrome snippets is still JavaScript RegExp, but replacing functionality is not the regular one.

    There are two approaches here:

    1. Use a lookbehind: (?<=m|n)0 => 1
    2. Use a named capturing group: (?<name>m|n)0 => $<name>1

    Do not forget to click the .* icon to enable regular expression replace mode!