0

I can not replace the string.

The String comes from a textarea

Here the JSFiddle

JSFiddle

value.replace('[Bold]', '<strong>');
Lukas -.-
  • 367
  • 3
  • 12

1 Answers1

-2

The problems with you code are

  • To handle some test text use g flag is RexExp
  • You [] and / should be escaped using \ in RegExp
  • Strings are immutable. You need to assign them new value. value.replace(...) will not change orginal string

    $('.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>&nbsp;</p>
<div class="description" >Texts Comes here</div>
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73