是否有任何库或自定义函数可以找到文本并将其设为粗体,即 wiki 文本到 html 的作品。实际上我想处理一个文本,它应该像 Whatsapp 和 Skype 的工作方式一样工作。
var textString = "A text which should be *bold* _Italic_ and ~Strike~ ";
我希望它在*,_和〜符号的基础上用jquery将单词粗体和斜体加粗到单词斜体
最佳答案
let textString = "A text which should be *bold* _Italic_ and ~Strike~ ";
const check = [{
reg: /\*(.*)\*/gm,
class: 'bold'
}, {
reg: /_(.*)_/gm,
class: 'italic'
}, {
reg: /~(.*)~/gm,
class: 'strike'
}];
let newStr = '';
check.map(x => {
const regx = x.reg.exec(textString);
const replace = "<span class='" + x.class + "'>" + regx[1] + "</span>";
const spl = textString.split(regx[0]);
newStr = spl[0] + replace + spl[1];
textString = newStr;
});
document.getElementById('foo').innerHTML = newStr;
.italic{
font-style: italic;
}
.strike {
text-decoration: line-through;
}
.bold {
font-weight: bold;
}
<div id="foo"><div>
关于javascript - jquery 查找并替换字符串中的单词,使其在 estaric 和 之间加粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62316774/