从字符串中找到“ *”,“ **”和“`”,并用JavaScript,Typescript中的<strong> </strong>和<code> </cod替换

时间:2019-08-05 05:10:03

标签: javascript typescript ecmascript-6 ecmascript-7

我们需要处理一个“字符串”,并用<strong></strong>包围'*'和'**'之间的所有文本,类似地,用<code> </code>包围backTic之间的文本。现在,我已经编写了逻辑,它也可以正常工作,但是我敢肯定,必须有一种更好的方法,因为对于此简单的字符串处理任务来说,代码太多了。以下是我的代码。感谢任何建议。

输入 = "*within single star* and **within double start** and this is `backtick string`"

输出 = "<strong>within single star</strong> and <strong>within double start</strong> and this is <code>backtick string</code>"

transform(data: any) {
        if (data) {
            const processDblStar = (input) => {
                const regExforDoubleStar = /(\*\*)+/gi;
                let i = 0;
                const result = input.replace(regExforDoubleStar, (match, matchedStr, offset) => {
                    i++;
                    return i % 2 === 0 ? '</strong>' : '<strong>';
                });
                return result;
            };

            const processSingleStar = (input) => {
                const regExforSingleStar = /(\*)+/gi;
                let i = 0;
                const result = input.replace(regExforSingleStar, (match, matchedStr, offset) => {
                    i++;
                    return i % 2 === 0 ? '</strong>' : '<strong>';
                });
                return result;
            };

            const processBackTick = (input) => {
                const regExforBackTick = /(\`)+/gi;
                let i = 0;
                const result = input.replace(regExforBackTick, (match, matchedStr, offset) => {
                    i++;
                    return i % 2 === 0 ? '</code>' : '<code>';
                });
                return result;
            };

            const processPipeline = (functions) => (inputStr) => functions.reduce((result, fn) => fn(result), inputStr);

            const funcArr: Function[] = [];
            if (data.indexOf('`') >= 0) {
                funcArr.push(processBackTick);
            }
            if (data.indexOf('*') >= 0) {
                funcArr.push(processSingleStar);
            }
            if (data.indexOf('**') >= 0) {
                funcArr.push(processDblStar);
            }

            processPipeline(funcArr)(data);
        }
    }

2 个答案:

答案 0 :(得分:2)

您可以使用正则表达式将***之间的所有文本分组。使用该组,然后可以通过使用$1引用它来在替换字符串中使用它。我们也可以对反引号做同样的事情,但是,将匹配的组括在<code></code>标记中。

const str = '*within single star* and **within double start** and this is `backtick string`';

const proccessPipeline = s =>
   s.replace(/\*{1,2}(.*?)\*{1,2}/g, '<strong>$1</strong>')
    .replace(/`(.*?)`/g, '<code>$1</code>');

const res = proccessPipeline(str);
console.log(res);
document.body.innerHTML = res;

答案 1 :(得分:1)

不是最好的方法。但简短的代码。

var converter = new showdown.Converter();
var input = "*within single star* and **within double start** and this is `backtick string`";
var output = converter.makeHtml(input);
output = "\"" + output + "\""
output = output.replace(/<p>/g, "")
output = output.replace(/<\/p>/g, "")
output = output.replace(/<em>/g, "<strong>")
output = output.replace(/<\/em>/g, "</strong>")
console.log(output)
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.0/showdown.min.js"></script>