我正在尝试替换文本字符串中所有提及的 [b] 并将其替换为 <b>
.我在下面使用的问题是它替换了页面上的每个 [b],我只希望它更改我发送的文本字符串(“文本”)中的 [b]。
在正则表达式中去掉代表全局的 'g' 效果不是很好,因为它不会全部替换它们...
text = $("#input").val();
text = text.replace(new RegExp('(^|\\s|>)\\[b](\\S.*?\\S)\\[/b]($|\\s|<)', 'gim') , '$1<strong>$2</strong>$3');
有什么想法吗?谢谢
最佳答案
我不确定问题出在哪里,因为问题有点不清楚。您是否尝试用新的 html 标签替换文本区域内的内容?
以下代码目前正在生产中为我工作:
text = $("#contract_body").val(); // strore the current contents
text = text.replace(/\[b\](.*?)\[\/b\]/gim, "<strong>$1</strong>"); // replace bbcode with html tags
$("#contract_body").val(text); // update the textarea with the new string contents
希望对您有所帮助。
关于javascript - 正则表达式不工作 "within"javascript 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8193190/