[JavaScript] replaceAll, javascriptreplace in JavaScript
In JavaScript, there is no replaceAll, only replace. replace can only replace the first character in the character, and this replace does not support regular expressions to achieve the purpose of replaceAll.
However, you can write a replaceAll in JavaScript without writing it to str. replaceAll: the degree to which one point can be used. Write a string with the returned value as the string type and after processing. The format parameter is the string to be processed, the functions of the things to be replaced and the things to be replaced.
Write a replaceAll statement in JavaScript to read data from the database for some multi-row text boxes. After that, the multi-row text boxes cannot be cleaned up by HTML functions in javascript, for more information, see [JavaScript] how to hide certain characters that can cause webpage crash and display that involves escape characters (click to open the link) and clear <BR> As \ r \ n, in this case, the multi-line text box is displayed normally. This is mostly seen in some editing functions.
The replaceAll function of JavaScript is as follows:
function replaceAll(str,replaced,replacement){var reg=new RegExp(replaced,"g");str=str.replace(reg,replacement);return str;}
An example is used to describe the usage of this function,
For example, the website has the following forms, which are displayed as if they were replaced by word.
The layout of this webpage is as follows, which is very simple. Pay attention to the ID in it:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
The GO button first calls the replaceAllTest function in the script to take the content in the text box, the content to be replaced, and the content to be replaced. Call the replaceAll () function to replace the text in the text box.
<script>function replaceAllTest(){var str=document.getElementById("str").value;var replaced=document.getElementById("replaced").value;var replacement=document.getElementById("replacement").value;str=replaceAll(str,replaced,replacement);document.getElementById("str").value=str;}function replaceAll(str,replaced,replacement){var reg=new RegExp(replaced,"g");str=str.replace(reg,replacement);return str;}</script>