简体   繁体   English

Whatsapp文本格式转换成html格式

[英]Whatsapp text format convert into html format

Need help to convert whatsapp text format to html format and convert html format to whatsapp text format.需要帮助将 whatsapp 文本格式转换为 html 格式并将 html 格式转换为 whatsapp 文本格式。 Using php script or javascript.使用 php 脚本或 javascript。

*bold* to <strong>bold</strong>
<strong>bold</strong> to *bold*

_italic_ to <em>italic</em>
<em>italic</em> to _italic_

~strike~ to <del>strike</del>
<del>strike</del> to ~strike~

Try this:尝试这个:

 let strBold = '*bold*'; let replacedBold = strBold.replace(/\\*(?=\\w)/, '<strong>').replace(/(?<=\\w)\\*/, '</strong>') console.log(replacedBold); let strItalic = '_italic_'; let replacedItalic = strItalic.replace(/\\_(?=\\w)/, '<em>').replace(/(?<=\\w)\\_/, '</em>'); console.log(replacedItalic); let strStrike = '~strike~'; let replacedStrike = strStrike.replace(/\\~(?=\\w)/, '<del>').replace(/(?<=\\w)\\~/, '</del>'); console.log(replacedStrike);

Just insert the symbol and the html tag in the htmlFormat object and it will replace it on the string.只需在 htmlFormat 对象中插入符号和 html 标签,它就会在字符串上替换它。

let string = 'hello I am *bold* text, and I am _italic_, I am ~line-through~ I am *bold again!*';

// html formatter
const htmlFormat = [
    { symbol: '*', tag: 'b' },
    { symbol: '_', tag: 'em' },
    { symbol: '~', tag: 'del' },
    { symbol: '`', tag: 'code' },
];

htmlFormat.forEach(({ symbol, tag }) => {
    if(!string) return;

    const regex = new RegExp(`\\${symbol}([^${symbol}]*)\\${symbol}`, 'gm');
    const match = string.match(regex);
    if(!match) return;

    match.forEach(m => {
        let formatted = m;
        for(let i=0; i<2; i++){
            formatted = formatted.replace(symbol, `<${i > 0 ? '/' : ''}${tag}>`);
        }
        string = string.replace(m, formatted);
    });
});

console.log(string); // hello I am <b>bold</b> text, and I am <em>italic</em>, I am <del>line-through</del> I am <b>bold again!</b>

Choose the perfect regex that will fit your needs.选择适合您需求的完美正则表达式。 If you don't want styling to span through new line and also using ([^*<\\n]+) makes sure at least one character is in between styles or else ** without a character in-between will result will become invisible.如果您不希望样式跨越新行并使用([^*<\\n]+)确保样式之间至少有一个字符,否则**中间没有字符将导致不可见.

function format_text(text){
return text.replace(/(?:\*)([^*<\n]+)(?:\*)/g, "<strong>$1</strong>")
     .replace(/(?:_)([^_<\n]+)(?:_)/g, "<i>$1</i>")
      .replace(/(?:~)([^~<\n]+)(?:~)/g, "<s>$1</s>")
      .replace(/(?:```)([^```<\n]+)(?:```)/g, "<tt>$1</tt>")
}

•The downside to the above code is that, you can't nest styles ie *_Bold and italic_* • 上面代码的缺点是,你不能嵌套样式,即*_Bold and italic_*

To allow nested styles use this 👇允许嵌套样式使用这个👇

format_text(text){
return text.replace(/(?:\*)(?:(?!\s))((?:(?!\*|\n).)+)(?:\*)/g,'<b>$1</b>')
   .replace(/(?:_)(?:(?!\s))((?:(?!\n|_).)+)(?:_)/g,'<i>$1</i>')
   .replace(/(?:~)(?:(?!\s))((?:(?!\n|~).)+)(?:~)/g,'<s>$1</s>')
   .replace(/(?:--)(?:(?!\s))((?:(?!\n|--).)+)(?:--)/g,'<u>$1</u>')
   .replace(/(?:```)(?:(?!\s))((?:(?!\n|```).)+)(?:```)/g,'<tt>$1</tt>');

// extra:
// --For underlined text--
// ```Monospace font```
}

👆 If you want your style to span through new line, then remove \\n from the regex. 👆 如果您希望您的样式跨越新行,请从正则表达式中删除\\n Also if your new line is html break tag, you can replace \\n with <br>此外,如果您的新行是 html break 标记,则可以将\\n替换为<br>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM