I need a jquery code where I need to search for an element and then replace every period (\.) with the text "[this is period]". How can I do this jquery search and replace?
var e = $('#container').text();
e.replace(/\./g,"[this is period]");
Jquery replace() takes first argument as the regular expression. So, the code below means that we are trying to replace all (/g) occurrence of text in #container with the text.
e.replace(/\./g,"[this is period]");
Therefore, the complete JQuery code looks like this:
<!DOCTYPE html><html><head><script src="http://code.jquery.com/jquery-latest.js"></script></head><body><div id="container">Thisis test for JQuery Find and Replace. This should work correctly.
<script>var e = $('#container').text();
e.replace(/\./g,"[this is period]");</script></body></html>
This would give output:
This is test for JQuery Find and Replace[this is period] This should work correctly[this is period]
You need to use Jquery replace() API function:
Jquery replace() takes first argument as the regular expression. So, the code below means that we are trying to replace all (/g) occurrence of text in #container with the text.
Therefore, the complete JQuery code looks like this:
This would give output:
Really Awesome and Thanks