JavaScript string Insert, delete, replace function use sample _javascript tips

Source: Internet
Author: User
Description:

The first two functions in the following function take out the first and last parts of the lookup string for use in other functions. Note that once the replacestring (MAINSTR,SEARCHSTR,REPLACESTR) function is invoked, only one searchstr string found in the string mainstr is replaced with a replacestr string, and the string cannot be All SEARCHSTR strings in Mainstr are replaced with replacestr strings, and if you need to replace them all, you need to use loops.

function Source:
[Code
Extracts all the characters in front of the lookup string
function Getfront (MAINSTR,SEARCHSTR) {
Foundoffset=mainstr.indexof (SEARCHSTR);
if (foundoffset==-1) {
return null;
}
Return mainstr.substring (0,foundoffset);
}
[/code]
Copy Code code as follows:

Extracts all characters following a lookup string
function Getend (MAINSTR,SEARCHSTR) {
Foundoffset=mainstr.indexof (SEARCHSTR);
if (foundoffset==-1) {
return null;
}
Return mainstr.substring (foundoffset+searchstr.length,mainstr.length);
}

Copy Code code as follows:

Inserts a string before the string Searchstr insertstr
function insertstring (MAINSTR,SEARCHSTR,INSERTSTR) {
var front=getfront (MAINSTR,SEARCHSTR);
var end=getend (MAINSTR,SEARCHSTR);
if (front!=null && end!=null) {
return front+insertstr+searchstr+end;
}
return null;
}

Copy Code code as follows:

Delete String deletestr
function deletestring (MAINSTR,DELETESTR) {
Return replacestring (Mainstr,deletestr, "");
}

Copy Code code as follows:

Modify the string searchstr to Replacestr
function replacestring (MAINSTR,SEARCHSTR,REPLACESTR) {
var front=getfront (MAINSTR,SEARCHSTR);
var end=getend (MAINSTR,SEARCHSTR);
if (front!=null && end!=null) {
return front+replacestr+end;
}
return null;
}

Use examples:
Suppose you have a form that you can use to receive message messages from users. We need to replace the user input in the message content with the HTML tag <br&gt, and also need to replace the spaces, so that when the message message can be displayed according to the user entered the original format of the display.
The HTML file is as follows:
Copy Code code as follows:

<script language= "JavaScript" >
This is populated by the scripting functions provided above, namely Getfront, Getend, replacestring
[, Insertstring, deletestring].
Form detection function
function Checkform (form) {
var gb_contentstr=form.elements["Gb_content"].value;
Replace all spaces in the message content with the
while (Gb_contentstr.indexof ("")!=-1) {
Gb_contentstr=replacestring (Gb_contentstr, "", "");
}
Replace all carriage returns in the message content with the <br>
while (Gb_contentstr.indexof ("\ r \ n")!=-1) {
Gb_contentstr=replacestring (Gb_contentstr, "\ r \ n", "<br>");
}
form.elements["Gb_content"].VALUE=GB_CONTENTSTR; Save the message content modified by the above script
return true; Submit Message
}
</script>
<body>
<form action= "writepro.asp" method= "post" name= "Addliuyan" onsubmit= "return Checkform (This)" >
<table width= "50%" border= "1" cellspacing= "0" cellpadding= "0" align= "center" >
<tr valign= "Middle" >
&LT;TD width= "15%" height= "align=" "right" > Message content:</td>
&LT;TD width= "35%" height= "align=" "Center" >
<textarea style= "Overflow:auto; width:100%, "name=" Gb_content "rows=" ></textarea>
</td>
</tr>
</table>
</form>
</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.