JSONATA:较低木制的单词

发布于 2025-01-23 06:34:06 字数 293 浏览 0 评论 0原文

我有一个由单词和标点符号组成的字符串,例如“接受数据保护条款 /条件(德语)”。我需要将其标准化为骆驼,以删除标点符号。

到目前为止,我最接近的尝试未能使单词骆驼,我只是设法使它们成为烤肉串或蛇case:

$normalizeId := function($str) <s:s> {
        $str.$lowercase()
            .$replace(/\s+/, '-')
            .$replace(/[^-a-zA-Z0-9]+/, '')
};

I have a string consisting of words and punctuation, such as "Accept data protection terms / conditions (German)". I need to normalize that to camelcase, removing punctuation.

My closest attempt so far fails to camelcase the words, I only manage to make them into kebab-case or snake_case:

$normalizeId := function($str) <s:s> {
        $str.$lowercase()
            .$replace(/\s+/, '-')
            .$replace(/[^-a-zA-Z0-9]+/, '')
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抽个烟儿 2025-01-30 06:34:06

Anindya的答案适用于您的示例输入,但是如果(German)未大写,则会导致不正确的输出:

"acceptDataProtectionTermsConditionsgerman"

链接到Playground


? cslokeslfenvvqmwfok33k6tpfecpufurkh8h1zvssrrfma1qspfjhxtpf1unxu% 2frdxhv1vjxntw0bzdezqvzdezqvzdezqvzdezqvzdezqvzbuyxdwigtwigtwigtwigtwigtwigt8ppnpnpnptnpnptnpntnptngngngng gugng gug 2g; 2x2uf

(
  $normalizeId := function($str) <s:s> {
        $str
            /* normalize everything to lowercase */
            .$lowercase()
            /* replace any "punctuations" with a - */
            .$replace(/[^-a-zA-Z0-9]+/, '-')
            /* Find all letters with a dash in front,
               strip the dash and uppercase the letter */
            .$replace(/-(.)/, function($m) { $m.groups[0].$uppercase() })
            /* Clean up any leftover dashes */
            .$replace("-", '')
  };

  $normalizeId($)
  /* OUTPUT: "acceptDataProtectionTermsConditionsGerman" */
)

% "https://www.stedi.com/jsonata/playground?statev2=eJx9Uu1qwjAUfZVLKNi6pnVfP%2BY%2BQAaDvcKaDkJ7tYGahCSdU%2Bu7L22d4hzePyEn95x77iFZRqxqTIEkJvitDVorlPQX3k7aa38yMisK1A5K7jhooxwWzreAQ7O0kEKhZCk6xEK48BiXESOeGDIJEEjlkVps8L2E6TPMG9mzw8A6E8GTndoX2HadQ3Xw8dZVOoaDBuAXmrWrhFyAU1CrFZqCW4RxekpKgsNbGJ3pGdQ1LxC4XAMjurPU8H4DRmAlXAUc6D%2Bae16YZp%2BU082MfkzoQ36VxjCio%2FMxb0KWwOsaanQ%2BLPsrXXJbgZAwN0q6%2BJTmyycgNLgKh0buRRqt94t28CB3yR8Nk8i7Ooa9jGALwTJZGNVom03yJDhohhHszs2%2F1silH9yHVOPcKR997wjthdGMUEZ8HkMcu0cm%2F%2F6CMAj8W%2BQ%2FiGpv2tv2juQxuSf5D3ApvQA%3D" rel=" nofollow noreferrer“>链接到游乐场

Anindya's answer works for your example input, but if (German) was not capitalized, it would result in the incorrect output:

"acceptDataProtectionTermsConditionsgerman"

Link to playground


This version would work and prevent that bug:

(
  $normalizeId := function($str) <s:s> {
        $str
            /* normalize everything to lowercase */
            .$lowercase()
            /* replace any "punctuations" with a - */
            .$replace(/[^-a-zA-Z0-9]+/, '-')
            /* Find all letters with a dash in front,
               strip the dash and uppercase the letter */
            .$replace(/-(.)/, function($m) { $m.groups[0].$uppercase() })
            /* Clean up any leftover dashes */
            .$replace("-", '')
  };

  $normalizeId($)
  /* OUTPUT: "acceptDataProtectionTermsConditionsGerman" */
)

Link to playground

ペ泪落弦音 2025-01-30 06:34:06

您应该针对前面有空间的字母,并使用此正则/\ s(。)/来大写。

这是我的片段:(编辑

(
    $upper := function($a) {
        $a.groups[0].$uppercase()
    };
    
    $normalizeId := function($str) <s:s> {
        $str.$lowercase()
            .$replace(/[^-a-zA-Z0-9]+/, '-')
            .$replace(/-(.)/, $upper)
            .$replace(/-/, '')
    };

    $normalizeId("Accept data protection terms / conditions (German)");
)

/* OUTPUT: "acceptDataProtectionTermsConditionsGerman" */

编辑:谢谢@vitorbal。不需要较早的正则替换功能“ $ lower”功能,也没有处理您提到的方案。感谢您指出的。我已经更新了片段,并添加了下面的操场的链接。

链接到操场

You should target the letters which has a space in front, and capitalize them by using this regex /\s(.)/.

Here is my snippet: (Edited

(
    $upper := function($a) {
        $a.groups[0].$uppercase()
    };
    
    $normalizeId := function($str) <s:s> {
        $str.$lowercase()
            .$replace(/[^-a-zA-Z0-9]+/, '-')
            .$replace(/-(.)/, $upper)
            .$replace(/-/, '')
    };

    $normalizeId("Accept data protection terms / conditions (German)");
)

/* OUTPUT: "acceptDataProtectionTermsConditionsGerman" */

Edit: Thanks @vitorbal. The "$lower" function on regex replacement earlier was not necessary, and did not handle the scenario you mentioned. Thanks for pointing that out. I have updated my snippet as well as added a link to the playground below.

Link to playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文