ReplaceAll in "javascript" javascript

Source: Internet
Author: User

There is no replaceall in JavaScript, only Replace,replace can replace the first character in the character, and the replace does not support regular expressions in order to achieve the purpose of replaceall.

But you can write a JavaScript in the ReplaceAll, do not write to Str.replaceall, a bit can be used to a degree of degree, write a return value is a string type, after processing the string, the formal parameter is the string to be processed, the object to be replaced, The function of the thing that is to be replaced is ready.

Write a JavaScript ReplaceAll you can read data from a database on some multiline text boxes, and then clean up a multiline text box without being able to clean up the HTML function in JavaScript, see "JavaScript" Some characters that are not escaped can cause the Web page to crash with the display method involving escape characters (click the Open link), <BR> clean up the problem to \ r \ n, thus displaying the problem in a multiline text box 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;}

Use a specific example to illustrate the use of this function,

For example, a Web site looks like a form that appears as a replacement feature in Word.


The layout of this page is as follows, very simple, please note the ID inside:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The Go button first invokes the Replacealltest function in the script, bringing the contents of the text box, the content being replaced, and the contents to be replaced. Simply call the ReplaceAll () function above and complete the replacement string, replacing 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);d Ocument.getelementbyid ("str"). Value=str; function ReplaceAll (str,replaced,replacement) {var reg=new RegExp (replaced, "G"); Str=str.replace (reg,replacement); return str;} </script>


ReplaceAll in "javascript" javascript

Related Article

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.