Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
        }
    }
like image 522
Akash Avatar asked Jan 31 '26 06:01

Akash


2 Answers

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;
like image 105
Nick Parsons Avatar answered Feb 01 '26 19:02

Nick Parsons


not the best way to do this. but short code.

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>
like image 28
kcsujeet Avatar answered Feb 01 '26 19:02

kcsujeet