0

Guys when I try to replace some value in input text box, its replacing only first value and its not replacing entire conversation.

In Input Text box I have conversation looks like

User 1 Prefix_ago User 2 Prefix_ago User 1 Prefix_ago

I wants to replace only prefix_ago with :

function copy() {
  var text = document.getElementById('result1').value;
  document.getElementById('id1').innerHTML = text;
  $('#id1').each(function() {
    var text = $(this).text();
    $(this).text(text.replace('prefix_ago', ' : '));
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea class="result" id="result1" rows="10" cols="150" style="font-size:11px;resize: none; width:225px;"></textarea>
<button class="btn58" id="btn" onClick="copy()"> Copy & Replace</button>
<div id="id1">

</div>

I have value called Prefix_ago which is repeating multiple times in my paragraph I want replace that to colon " : " that code is working but its replacing only very first value not entire paragraph

  • 2
    Possible duplicate of [How to replace all occurrences of a string in JavaScript?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – mrogers Jun 12 '17 at 16:20
  • 2
    Why are you using `.each()` to loop over one element (`#id1`)? Just access that element directly. – Scott Marcus Jun 12 '17 at 16:20
  • `string.replace(str, str)` indeed replaces only the first occurence, contrary to `string.replace(regex, str)`, which can replace all, depending on flags. ↑↑The first answer of the question linked just two comments up is fairly comprehensive. – Boris Jun 12 '17 at 16:25

1 Answers1

0

The core of the answer is, you need to include the 'g' option on the regex for it to match more than one instance.

That said, you're overcomplicating your code quite a bit -- there's no need to $.each over a single output field, or to copy the text into the output field before changing it. Here's a simplified version:

function copy() {
  var txt = $('#result1').val().replace(/prefix_ago/g, ' : ');
  txt = txt.replace(/\n/g, '<br>'); // <-- per comments below
  $('#id1').html(txt);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="result1" rows="10" cols="150">
test prefix_ago test 
prefix_ago test    
test prefix_ago test 
prefix_ago test
</textarea>
<button onclick="copy()"> Copy & Replace</button>
<div id="id1">
</div>
Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
  • In case the downvote was because I didn't explain the answer sufficiently, I've updated. If that wasn't the reason, I'm open to suggestions! – Daniel Beck Jun 12 '17 at 16:25
  • Thanks its working perfectly .. I have conversations between users but this code just wrap up all lines into paragraph .. I am new to Jquery can you help me on that something \n or needs to be updated – Mahather Mohamed Jun 12 '17 at 16:31
  • Conversation looks like below but I am getting all results in one line user 1 prefix_ago user 2 prefix_ago user 1 prefix_ago user 2 prefix_ago user 1 prefix_ago user 2 prefix_ago – Mahather Mohamed Jun 12 '17 at 16:33
  • @MahatherMohamed That's because newlines don't mean anything in HTML. I updated the answer to replace those with `
    ` tags instead.
    – Daniel Beck Jun 12 '17 at 16:41
  • you are really awesome :) :) this code works perfect as expected thanks for your help :) :) you are really great @Daniel Beck – Mahather Mohamed Jun 12 '17 at 16:46
  • Glad I could help. – Daniel Beck Jun 12 '17 at 16:46
  • I have small query Daniel Is it possible to remove from particular line using # tag for example line looks like # 13m # test # 12m # test .. The result should be test test .. @Daniel Beck – Mahather Mohamed Jun 12 '17 at 16:57
  • If I'm understanding you correctly, that regex would be something like `/# \w*/g`. You might find this useful: https://regex101.com – Daniel Beck Jun 12 '17 at 17:01
  • So that code would be txt = txt.replace(/# \w*/g, '
    '); is that right @regex101.com
    – Mahather Mohamed Jun 12 '17 at 17:08