[JavaScript] replaceAll, javascriptreplace in JavaScript

Source: Internet
Author: User

[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>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.