使用正则表达式进行搜索文本高亮,即使文本中包含逗号。

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

Search text highlight using regex even the text has commas

问题

我遇到了在搜索HTML表格中的值时找到基于正则表达式的解决方案的问题。

在下面的屏幕截图中,名为"Amount"的列中可能在数字之间包含","。我们的客户要求,当他们在搜索字段中搜索"247"时,类似"2,47..."或列中的"247"这样的值都应该被突出显示。

我尝试使用正则表达式解决这个问题,但是没有达到预期的效果:

const regex = new RegExp(
  searchTerm
  ?.toString()
  .replace(/[.*+?^${}()|[\]\\]/g, "\$&")
  .replace(/\s+/g, "|") + ",?",
  "gi"
);

在前面的示例中,我如何突出显示"2,47"?

英文:

I'm having trouble finding a regular expression based solution to a problem involving searching for values in a HTML table.

In the screenshot further down below, the column named "Amount" may contain "," between the digits. It's our client's requirement that when they search for "247" (using the search field) values like "2,47..." or those like "247" in the column, should be highlighted.

I tried to solve it using regular expressions but it did not work as expected:

const regex = new RegExp(
  searchTerm
  ?.toString()
  .replace(/[.*+?^${}()|[\]\\]/g, "\$&")
  .replace(/\s+/g, "|") + ",?",
  "gi"
);

使用正则表达式进行搜索文本高亮,即使文本中包含逗号。

How do I highlight "2,47" in the preceding example?

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

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

const regex = new RegExp(
  247
  ?.toString()
  .replace(/[.*+?^${}()|[\]\\]/g, &quot;\$&amp;&quot;)
  .replace(/\s+/g, &quot;|&quot;) + &quot;,?&quot;,
  &quot;gi&quot;
);

console.log(&#39;247&#39;,&#39;247&#39;.match(regex))
console.log(&#39;2,47&#39;,&#39;2,47&#39;.match(regex))
console.log(&#39;2.47&#39;,&#39;2.47&#39;.match(regex))

<!-- end snippet -->

答案1

得分: 3

From your clarifications, I see no other choice than splitting your string and dynamically inserting an optional ,? between all your characters. Seems overkill but I don't see any other way to ignore the comas.

FINAL EDIT thanks to markalex:
Much cleaner version, the ,? are now added only between digits. This way it can be done at the end after escaping the special characters and doesn't need hackish tricks that could affect the robustness.

You also don't need to insert the last ,?, il will have no effect

const regex = new RegExp(
247
?.toString()
.replace(/[.*+?^${}()|[]\]/g, "\$&")
.replace(/(?<=\d)(?=\d)/g, ",?")
.replace(/\s+/g, "|"),
"gi"
);

console.log('regex: ',
247
?.toString()
.replace(/[.*+?^${}()|[]\]/g, "\$&")
.replace(/(?<=\d)(?=\d)/g, ",?")
.replace(/\s+/g, "|")
)

console.log('247','247'.match(regex))
console.log('2,47','2,47'.match(regex))
console.log('2.47','2.47'.match(regex))

const regex2 = new RegExp(
'question?'
?.toString()
.replace(/[.*+?^${}()|[]\]/g, "\$&")
.replace(/(?<=\d)(?=\d)/g, ",?")
.replace(/\s+/g, "|"),
"gi"
);

console.log('regex: ',
'question?'
?.toString()
.replace(/[.*+?^${}()|[]\]/g, "\$&")
.replace(/(?<=\d)(?=\d)/g, ",?")
.replace(/\s+/g, "|")
)

console.log('question?','question?'.match(regex2))
console.log('question','question'.match(regex2))

英文:

From your clarifications, I see no other choice than splitting your string and dynamically inserting an optional ,? between all your characters. Seems overkill but I don't see any other way to ignore the comas.

FINAL EDIT thanks to markalex:
Much cleaner version, the ,? are now added only between digits. This way it can be done at the end after escaping the special characters and doesn't need hackish tricks that could affect the robustness.

You also don't need to insert the last ,?, il will have no effect

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

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

const regex = new RegExp(
  247
  ?.toString()
  .replace(/[.*+?^${}()|[\]\\]/g, &quot;\$&amp;&quot;)
  .replace(/(?&lt;=\d)(?=\d)/g, &quot;,?&quot;)
  .replace(/\s+/g, &quot;|&quot;),
  &quot;gi&quot;
);

console.log(&#39;regex: &#39;,
  247
  ?.toString()
  .replace(/[.*+?^${}()|[\]\\]/g, &quot;\$&amp;&quot;)
  .replace(/(?&lt;=\d)(?=\d)/g, &quot;,?&quot;)
  .replace(/\s+/g, &quot;|&quot;)
)

console.log(&#39;247&#39;,&#39;247&#39;.match(regex))
console.log(&#39;2,47&#39;,&#39;2,47&#39;.match(regex))
console.log(&#39;2.47&#39;,&#39;2.47&#39;.match(regex))

const regex2 = new RegExp(
  &#39;question?&#39;
  ?.toString()
  .replace(/[.*+?^${}()|[\]\\]/g, &quot;\$&amp;&quot;)
  .replace(/(?&lt;=\d)(?=\d)/g, &quot;,?&quot;)
  .replace(/\s+/g, &quot;|&quot;),
  &quot;gi&quot;
);

console.log(&#39;regex: &#39;,
  &#39;question?&#39;
  ?.toString()
  .replace(/[.*+?^${}()|[\]\\]/g, &quot;\$&amp;&quot;)
  .replace(/(?&lt;=\d)(?=\d)/g, &quot;,?&quot;)
  .replace(/\s+/g, &quot;|&quot;)
)

console.log(&#39;question?&#39;,&#39;question?&#39;.match(regex2))
console.log(&#39;question&#39;,&#39;question&#39;.match(regex2))

<!-- end snippet -->

答案2

得分: 0

答案链接似乎覆盖了主要用例,但要考虑如果金额不仅以美元符号$表示,还可以用英镑符号£表示。这正是逻辑可能失败的地方。

另一种方法是将金额存储为数字格式,例如12345,或者字符串"12345"存储在数据库中。这将使在这些数据上进行查找/搜索查询变得非常简单和高效。

现在,为了在UI端进行格式化,比如显示为金额$XX,XXX,你可以轻松使用一个掩码函数。

这降低了逻辑的复杂性,也可以轻松地根据客户需求进行修改。

英文:

The answer, seems to be covering the main usecases, but consider if you have amount that is not only in $ dollars but can be in pounds. That is exactly where the logic can fail.

A different approach can be to store the amount in the format of a number as in 12345 or string "12345" in the DB. This would make it very easy and efficient to make a find/search query on this data.

Now for the purpose of formatting on UI side like a amount $XX,XXX. You can easily use a mask function.

This is reducing the complexity of logic and easily can be modified for furthur client requirements.

huangapple
  • 本文由 发表于 2023年4月17日 17:49:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033812.html
匿名

发表评论

匿名网友

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

确定