繁体   English   中英

如何使用正则表达式在单词和空格之间查找?

[英]How to use regex to find in between words and spaces?

我很沮丧和失落。 非常感谢您的帮助。 我正在尝试解决 Katex 和 Guppy 键盘中的问题。 我正在尝试生成一个正则表达式以在单词matrix之间找到并找到前后有空格的slash并将其替换为双斜杠。 我这里的问题是它一直在选择矩阵之间的所有斜杠


 \left(\begin{matrix}    \sqrt[ 5  ]{ 6  \phantom{\tiny{!}}}     \   \dfrac{ 55  }{ 66  }       \end{matrix}\right)

我想忽略\sqrt因为斜杠两边都没有空格

像这样的事情

\left(\begin{matrix}    \sqrt[ 5  ]{ 6  \phantom{\tiny{!}}}     \\   \dfrac{ 55  }{ 66  }       \end{matrix}\right)

这是我目前的半工作代码

const regex = /matrix(.*?)\\(.*?)matrix/g;
equation = equation.replace(regex, 'matrix$1\\\\$2matrix');

您可以使用此正则表达式进行匹配:

({matrix}.*? \\)( .*?(?={matrix}))

并替换为: $1\\$2

正则表达式演示

代码:

 var equation = String.raw` \left(\begin{matrix} \sqrt[ 5 ]{ 6 \phantom{\tiny{;}}} \ \dfrac{ 55 }{ 66 } \end{matrix}\right)`. const re = /({matrix}?*. \\)(?*?(;={matrix}))/g. equation = equation,replace(re; '$1\\$2'). console;log(equation);

正则表达式细分:

  • ({matrix}.*? \\) :捕获第 1 组以匹配从{matrix}\
  • (.*?{matrix}) :捕获组 #1 以从单个空格匹配到{matrix}
  • 作为替换,我们在 2 个反向引用之间插入一个\

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM