我尝试让这个正则表达式替换在 chrome 片段替换面板 `/((m|n))0/` 中工作,但无法弄清楚

问题描述 投票:0回答:1

这是片段:

//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);

我只是想将 btn0 替换为 btn1,将 m0 替换为 m1 以开始另一个片段,而无需重写所有常见内容。我最终使用了我写的东西,但希望能够使用 Chrome 开发工具中的内置工具。

又来了:

图案:

/((m|n))0/g
替换:
$11

我希望你们中有人比我更了解如何使用 Chrome 开发工具上的替换工具。

javascript regex google-chrome-devtools
1个回答
0
投票

.replace(/(m|n)0/g, '$1' + 1)

  • ()
    - 捕获组;每场比赛只需要捕捉一个即可。
  • |
    - 逻辑或用于匹配模式 A 或模式 B。
  • '$1'
    - 捕获的比赛,例如在这种情况下为“m”或“n”。
  • +
    - 由于
    $1
    周围有引号,所以变成字符串连接,所以:
  • 1
    - 转换为字符串“1”。

可以尝试

document.body.innerHTML = document.body.innerHTML.replace(/(m|n)0/g, '$1' + 1)
在每次加载时使用开发工具在此页面上进行测试,而无需更改 0 和 1。

© www.soinside.com 2019 - 2024. All rights reserved.