Need to replace the text starting with the word "Designation:" and upto the end

There is a tag inside it, there is some text, I need to replace the text starting with the text "Designation: SME" the Designation may change i.e. it may be "Designation : Senior CTO", So I need to select all the text starting with "Designation : ......" and replace it with some other text.

For e.g. the HTML is as shown below:

<span id="lblEmployee">EMP No: 123456789
  </br>
  </br>
  *Designation:&nbsp;&nbsp;&nbsp;Senior CTO*
</span>

Now, I need to replace the text inside the ** i.e. the text starting with "Designation....." up to the end i.e. CTO

Please suggest on what is the JQuery I can use to do this ?

Answers 1

  • You can use replace with regex:

    $(document).ready(function() {
      console.log($('#lblEmployee').text().replace(/Designation:.*/, 'nextext'))
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <span id="lblEmployee">EMP No: 123456789
      </br>
      </br>
      Designation:&nbsp;&nbsp;&nbsp;Senior CTO
    </span>


Related Articles