问题描述
我正在尝试替换文本字符串中的[b]的所有提及,并将其替换为< b>
。我在下面使用它的问题是它取代了页面上的每个[b],我只希望它改变我发送的文本字符串(text)中的[b]。
I'm trying to replace all mentions of [b] in a text string and replace it with <b>
. The problem with what I'm using below this that it's replace every [b] on the page, and I only want it to change the [b]'s within the text string ("text") that I'm sending in.
在正则表达式中取出全局'g'并不能很好地工作,因为它不会全部替换它们...
Taking out the 'g' for global in the regex doesn't work very well because it then doesn't replace them all...
text = $("#input").val();
text = text.replace(new RegExp('(^|\\s|>)\\[b](\\S.*?\\S)\\[/b]($|\\s|<)', 'gim') , '$1<strong>$2</strong>$3');
任何想法?谢谢
推荐答案
我不确定问题是什么,因为这个问题有点不清楚。您是否尝试使用新的html标签替换textarea中的内容?
I'm not exactly sure what the problem is because the question is a bit unclear. Are you trying to replace the content within the textarea with the new html tags?
以下代码目前适用于我的生产:
The following code is currently working for me in production:
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字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!