答案 0 :(得分:-1)
使用正则表达式替换每次出现的字符串。
const str = 'lorem ipsum [Bold] dolor sit [Bold] amet';
console.log(
str.replace(/\[Bold\]/g, '<strong>')
)
// result - lorem ipsum <strong> dolor sit <strong> amet
答案 1 :(得分:-2)
您的代码存在问题
g
标志为RexExp []
对/
和\
进行转义value.replace(...)
不会更改原始字符串
$('.description:not(.focus)').keyup(function(){
var value = $(this).val();
var contentAttr = $(this).attr('name');
value = value.replace(/\[Bold\]/g, '<strong>');
value = value.replace(/\[\/Bold\]/g, '</strong>');
value = value.replace(/\[Italic\]/g, '<em>');
value = value.replace(/\[\/Italic\]/g, '</em>');
value = value.replace(/\[Underline\]/g, '<u>');
value = value.replace(/\[\/Underline\]/g, '</u>');
$('.'+contentAttr+'').html(value.replace(/\r?\n/g,'<br/>'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
start typeing in textarea whatever text you type it will also apear in div which has class <strong>'content'</strong> but when I press enter for new line in textarea when i get the problem div named <strong>'content'</strong> will not making new line please help
<br/>
<textarea name="description" rows="15" class="description"></textarea>
<p> </p>
<div class="description" >Texts Comes here</div>