Javascript Whatsapp文本格式转换为html格式
需要帮助将whatsapp文本格式转换为html格式,并将html格式转换为whatsapp文本格式。使用php脚本或javascriptJavascript Whatsapp文本格式转换为html格式,javascript,php,html,jquery,css,Javascript,Php,Html,Jquery,Css,需要帮助将whatsapp文本格式转换为html格式,并将html格式转换为whatsapp文本格式。使用php脚本或javascript *bold* to <strong>bold</strong> <strong>bold</strong> to *bold* _italic_ to <em>italic</em> <em>italic</em> to _italic_ ~strike~ t
*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~
*加粗*至加粗
加粗至*加粗*
_斜体到斜体
斜体到_斜体_
~strike~去罢工
挨打~
试试这个:
让strBold='*bold*';
让replacedBold=strBold.replace(/\*(?=\w)/,“”)。replace(/(?)只需在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>
let string='您好,我是*粗体*文本,我是_italic,我是~line-through~我又是*粗体!*';
//html格式化程序
常量htmlFormat=[
{符号:'*',标记:'b'},
{symbol:''''u',标记:'em'},
{符号:'~',标记:'del'},
{符号:'`',标记:'code'},
];
htmlFormat.forEach(({symbol,tag})=>{
如果(!string)返回;
const regex=new RegExp(`\\${symbol}([^${symbol}]*)\\${symbol}`,'gm');
const match=string.match(regex);
如果(!匹配)返回;
match.forEach(m=>{
设=m;
for(设i=0;i`);
}
string=string.replace(m,格式化);
});
});
console.log(string);//您好,我是粗体文本,我是斜体,我又是粗体了!