如何使用< sup&gt格式化半尺寸标签?

发布于 2025-01-21 05:59:05 字数 685 浏览 3 评论 0原文

以下是连接单词的功能。鉴于第一个或最后一个单词可以是像7 1/2这样的字符串,我将如何确保;如果单词包含一个分数,请用(SuperScript)标签格式化分数。

export const joinWords = (words: string[]): string => {
    if (words.length === 1) {
        return words[0];
    }
    const firstWords = words.slice(0, words.length - 1);
    const lastWord = words[words.length - 1];
    const oxfordComma = words.length > 2;

    //if firstWords or lastword contains "/"...Logic???

    return firstWords.join(', ') + `${oxfordComma ? ', ' : ''} and ${lastWord}`;
};

Below is a function for joining words. Given the first or last word could be a string like 7 1/2, how would I ensure that; if the word contains a fraction, format the fraction with (superscript) tags.. so it shows nicely like below?

export const joinWords = (words: string[]): string => {
    if (words.length === 1) {
        return words[0];
    }
    const firstWords = words.slice(0, words.length - 1);
    const lastWord = words[words.length - 1];
    const oxfordComma = words.length > 2;

    //if firstWords or lastword contains "/"...Logic???

    return firstWords.join(', ') + `${oxfordComma ? ', ' : ''} and ${lastWord}`;
};

enter image description here

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

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

发布评论

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

评论(1

找个人就嫁了吧 2025-01-28 05:59:05

我找到了一种替代方法,其中使用替换方法。我正在使用Svelte,因此很大。

onMount(() => {
//join the words
        const test = joinWords(optionNames);
//replace the "1/2" since I am only using 1/2 for clothing sizes...this may not work for other fractions..
        const newTest = test.replace('1/2', '½');
        testWord = newTest;
    });
//be sure to return as html...again im in svelte.
<span>{@html testWord} </span><br />

I found an alternate way, where I use the replace method. I am using svelte hence the onmount..

onMount(() => {
//join the words
        const test = joinWords(optionNames);
//replace the "1/2" since I am only using 1/2 for clothing sizes...this may not work for other fractions..
        const newTest = test.replace('1/2', '½');
        testWord = newTest;
    });
//be sure to return as html...again im in svelte.
<span>{@html testWord} </span><br />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文