0

I am trying to Replace Old Values with New Values in P Tag By Using JQuery replace(String SearchValue, String replaceValue) Method on buton click. if I pass Fixed Values then it update but when I Pass through input text Box then it doesn't.

I used Different method to Solve this problem even tried on internet but no salutation .

 <div class="container">
        <div class="row">
            <input type="text" value="" id="TxtOne" name="TxtOne" placeholder="Old Word" class="form-control" />
        </div>
        <div class="row">
            <input type="text" value="" id="TxtTwo" name="TxtTwo" placeholder="New Word" class="form-control" />
        </div>
        <div class="row">
            <input type="button" onclick="ChangeData();" class="btn btn-primary" value="Update" id="btnOne" name="btnOne" style="float:right" />
        </div>
    </div>

I want to replace hate with Love

<script>
    function ChangeData() {
        var oldData = $('#TxtOne').val();
        oldData = '/' + oldData + '/ig';
        var newData = $('#TxtTwo').val();
        var $p = $('p');
        $p.text($p.text().replace(oldData, newData));
        //$p.text($p.text().replace(/hate/ig, 'love'));
    };
</script>

No Result

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64

1 Answers1

0

Your issue is that you cannot build a regular expression by doing:

oldData = '/' + oldData + '/ig';

Instead, you can use the RegExp constructor to build your expression and use that in your replace method.

Also, here I am passing an arrow function into the .text() method which will set the text equal to whatever it returns.

See example below:

$("#btnOne").click(ChangeData);

function ChangeData() {
  var oldData = new RegExp($('#TxtOne').val(), 'ig');
  var newData = $('#TxtTwo').val();
  $('p').text((_, txt) => txt.replace(oldData, newData));
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <p>I want to replace hate with love</p>
  <div class="row">
    <input type="text" value="" id="TxtOne" name="TxtOne" placeholder="Old Word" class="form-control" />
  </div>
  <div class="row">
    <input type="text" value="" id="TxtTwo" name="TxtTwo" placeholder="New Word" class="form-control" />
  </div>
  <div class="row">
    <input type="button" class="btn btn-primary" value="Update" id="btnOne" name="btnOne" style="float:right" />
  </div>
</div>

Please note, however, the RegExp creates a regular expression, and so it may behave unexpectedly if special characters are used in the input.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • this will only work if `oldData` doesn't contain any characters that would have a special meaning when used in a regular expression. – Andreas Mar 30 '19 at 13:02
  • 1
    Yes Now it works fine, Thanks for solving my Code Problem Mr. Nick Parsons, – Premjit Gill Mar 30 '19 at 13:06
  • [Is there a RegExp.escape function in Javascript?](https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript) – Andreas Mar 30 '19 at 13:16