1

I'm currently translating some C# code into JavaScript and I got stuck in one thing. What is the equivalent in JavaScript for:

if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
{
  // Do something
}

I'm not being able to understand if we have this in JavaScript.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
GNuno
  • 51
  • 4
  • 11
  • I doubt there is anything equivalent – epascarello Jul 06 '20 at 17:05
  • Can you explain to me more or less the validation that is being made, since I never used this in the past? – GNuno Jul 06 '20 at 17:13
  • There are [Unicode property escapes in regex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes)... – Heretic Monkey Jul 06 '20 at 17:33
  • My answer would be better if there was a [mre] in the question, showing a value of `c` that causes the condition to be true and one that causes the condition to be false... – Heretic Monkey Jul 06 '20 at 17:45
  • I think this code in C# is checking that the character doesn't have non spacing characters (like e.g. Spanish accents á, é, í, ó, ú). Check out this [How to remove accents/diacritics in .NET](https://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in) and [Remove accents/diacritics in JavaScript](https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript). The second link would tell you how to detect those characters in different languages using Javascript. – derloopkat Jul 06 '20 at 17:53
  • @derloopkat the second link just fixed my problem. Thank you so much! – GNuno Jul 07 '20 at 08:36

2 Answers2

0

As per @derloopkat help, I was able to solve this with the solution present in this post:

Thank you all for your help!

    const str = "Crème Brulée"
    str.normalize("NFD").replace(/[\u0300-\u036f]/g, "")
    > "Creme Brulee"
GNuno
  • 51
  • 4
  • 11
-1

The UnicodeCategory enumeration appears to map to the General Category listing of the Unicode Character Database. The Unicode property escapes feature of JavaScript's regular expression engine appears able to map those General Categories.

For instance, the example given,

if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
{
  // Do something
}

would map to

if (!/\p{General_Category=Nonspacing_Mark}/.test(c)) {
  // Do something
}

But I believe it could be shortened to

if (!/\p{Nonspacing_Mark}/.test(c)) {
  // Do something
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122