JS Replace function function

Source: Internet
Author: User
function fn ()
{
for (var i = 0;i < arguments.length;i++) {
Alert ("First" + (i+1) + "value of parameter:" +arguments[i]);
}
}
var str = ' <div id= ' {wo} ' >{ni}</div> ';
Str.replace (/{([a-z]+)}/ig, FN);

The first parameter is a matching string, such as {wo} and {ni};

The second argument can have 0-n, matching the first argument to a regular string of parentheses, such as Wo and NI in the first argument, to match ([a-z]+),

There are several groups of parentheses, then there are several parameters;

The third parameter is the location of the string to which the first argument matches, such as {wo} returning 9,{no} to return 16;

The fourth parameter is the string used to match, in this case the <div id= "{wo}" >{NI}</DIV>.

Look at an example

<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<title> New Document </title>
<meta name= "generator" content= "EditPlus" >
<meta name= "Author" content= "" >
<meta name= "keywords" content= "" >
<meta name= "description" content= "" >
<script language= "Web Effects" type= "Text/javascript" >
String.prototype.replaceall = Stringreplaceall;
function Stringreplaceall (afindtext,areptext) {
var raregexp = new RegExp (afindtext.replace (/) [() []{}^$+-*?.] /]/g, "$"), "IG");
Return This.replace (Raregexp,areptext);
}
function Myreplace () {
var Content=document.getelementbyid ("Content"). Value;
var rel_con=content.replaceall ("$name", "wwww");
document.getElementById ("Content2"). Value=rel_con;
}
</script>
<body>
<input type= "text" id= "content" Name= "Contxt" value= "$name Customer Name"/> <input "button" type= "Replace"
Onclick= "Myreplace ()"/><br/>
<input type= "text" id= "Content2" value= ""/>
</body>

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.