Regular Expressions has two string methods: search() and replace(). The search() method uses an expression to search for a match, and returns the position of the match.
1 2 | var msg = "Hello John"; var n = msg.search(/John/i); // 6 |
The replace() method is used to return a modified string where the pattern is replaced.
1 2 3 4 | var msg = "Hello John"; // Hello Buttler var n = msg.replace(/John/i, "Buttler"); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.