请帮我处理正则表达式。

huangapple go评论57阅读模式
英文:

Please help me with regular expression

问题

正则表达式不起作用。
我需要标记字符串的一部分,但我不知道应该怎么做。

const markPartOfString = (string, substrings) => {
  if (!substrings) return string;
  const parts = substrings.split(',').map(str => str.trim());

  let markedString = string;
  parts.forEach(part => {
    const regex = new RegExp(`\\b${part}\\b`, 'gi');
    markedString = markedString.replace(regex, match => `<mark>${match}</mark>`);
  });

  return markedString;
}

// 期望的结果
console.log(markPartOfString("wfo test3", "wfo test, wfo test3, wf te")); // <mark>wfo</mark><mark>test3</mark>
console.log(markPartOfString("wfo test", "wf")); // <mark>wf</mark>0 test
console.log(markPartOfString("wfo test", "wf te")); // <mark>wf</mark>o <mark>te</mark>st
console.log(markPartOfString("wfo test", "wfo")); // <mark>wfo</mark> test
console.log(markPartOfString("wfo test", "o st")); // wf<mark>o</mark> <mark>st</mark>est
英文:

Regular expression not working correctly.
I need to mark part of string, but I don't know how it is do

const markPartOfString = (string, substrings) =&gt; {
  if (!substrings) return string;
  const parts = substrings.split(&#39;,&#39;).map(str =&gt; str.trim());

  let markedString = string;
  parts.forEach(part =&gt; {
    const regex = new RegExp(`\\b${part}\\b`, &#39;gi&#39;);
    markedString = markedString.replace(regex, match =&gt; `&lt;mark&gt;${match}&lt;/mark&gt;`);
  });

  return markedString;
}

//Expected result 
console.log(markPartOfString(&quot;wfo test3&quot;, &quot;wfo test, wfo test3, wf te&quot;)); // &lt;mark&gt;wfo&lt;/mark&gt;&lt;mark&gt;test3&lt;/mark&gt;
console.log(markPartOfString(&quot;wfo test&quot;, &quot;wf&quot;)); // &lt;mark&gt;wf&lt;/mark&gt;0 test
console.log(markPartOfString(&quot;wfo test&quot;, &quot;wf te&quot;)); // &lt;mark&gt;wf&lt;/mark&gt;o &lt;mark&gt;te&lt;/mark&gt;st
console.log(markPartOfString(&quot;wfo test&quot;, &quot;wfo&quot;)); // &lt;mark&gt;wfo&lt;/mark&gt; test
console.log(markPartOfString(&quot;wfo test&quot;, &quot;o st&quot;)); // wf&lt;mark&gt;o&lt;/mark&gt; &lt;mark&gt;st&lt;/mark&gt;est

答案1

得分: 1

我认为以下示例有效,除了第一个情况,它不会产生相同的结果,因为在输入中存在逗号时,不清楚期望的行为是什么。

另外:

  1. 不要忘记转义在正则表达式中使用的动态值
  2. 不要忘记转义在HTML中使用的动态值

希望的结果:

console.log(markPartOfString("wfo test3", "wfo test, wfo test3, wf te")); // <mark>wfo</mark><mark>test3</mark>
console.log(markPartOfString("wfo test", "wf")); // <mark>wf</mark>0 test
console.log(markPartOfString("wfo test", "wf te")); // <mark>wf</mark>o <mark>te</mark>st
console.log(markPartOfString("wfo test", "wfo")); // <mark>wfo</mark> test
console.log(markPartOfString("wfo test", "o st")); // wf<mark>o</mark> <mark>st</mark>est
英文:

I think the below example works, except the first case which doesn't produce the same result, because it's not clear what the desired behaviour is when there are commas in the input.

Also:

  1. Don't forget to escape the dynamic values used in a RegExp
  2. Don't forget to escape the dynamic values used in HTML

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
function escapeRegExp(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, &#39;\$&amp;&#39;);
}

// https://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript
function escapeHtml(str) {
  return str
    .replace(/&amp;/g, &quot;&amp;amp;&quot;)
    .replace(/&lt;/g, &quot;&amp;lt;&quot;)
    .replace(/&gt;/g, &quot;&amp;gt;&quot;)
    .replace(/&quot;/g, &quot;&amp;quot;&quot;)
    .replace(/&#39;/g, &quot;&amp;#039;&quot;);
 }

const markPartOfString = (string, substrings) =&gt; {
  if (!substrings) return string;
  
  const parts = substrings.split(&#39;,&#39;)
    .flatMap(str =&gt; str.split(&#39; &#39;))
    .map(str =&gt; escapeRegExp(str.trim()))
    .filter(str =&gt; !!str);

  const regex = new RegExp(`(${parts.join(&#39;|&#39;)})`, &#39;gi&#39;);

  return string.replace(regex, match =&gt; `&lt;mark&gt;${escapeHtml(match)}&lt;/mark&gt;`);
}

//Expected result 
console.log(markPartOfString(&quot;wfo test3&quot;, &quot;wfo test, wfo test3, wf te&quot;)); // &lt;mark&gt;wfo&lt;/mark&gt;&lt;mark&gt;test3&lt;/mark&gt;
console.log(markPartOfString(&quot;wfo test&quot;, &quot;wf&quot;)); // &lt;mark&gt;wf&lt;/mark&gt;0 test
console.log(markPartOfString(&quot;wfo test&quot;, &quot;wf te&quot;)); // &lt;mark&gt;wf&lt;/mark&gt;o &lt;mark&gt;te&lt;/mark&gt;st
console.log(markPartOfString(&quot;wfo test&quot;, &quot;wfo&quot;)); // &lt;mark&gt;wfo&lt;/mark&gt; test
console.log(markPartOfString(&quot;wfo test&quot;, &quot;o st&quot;)); // wf&lt;mark&gt;o&lt;/mark&gt; &lt;mark&gt;st&lt;/mark&gt;est

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 02:33:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定