Search code examples
javascripttypescriptecmascript-6ecmascript-2016

find '*' , '**' and '`' from string and replace with <strong></strong> and <code></cod in javascript, Typescript


we need to process an "string" and surround all text between '*' and '**' with <strong></strong> and similarly the text between backTic with <code> </code>. Now I have written the logic, and it works fine as well, but I am sure, there must be a better way as it is too much code for this simple task of string processing. Following is my code. Appreciate any suggestions.

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

output = "<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);
        }
    }

Solution

  • You can use regex to group all the text between ** and *. Using this group, you can then use it in your replacing string by referencing it using $1. We can also do the same thing with the backticks, however, instead, surround the matched group in <code></code> tags.

    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;