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>, 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" >
<TD width= "15%" height= "align=" "right" > Message content:</td>
<TD width= "35%" height= "align=" "Center" >
<textarea style= "Overflow:auto; width:100%, "name=" Gb_content "rows=" ></textarea>
</td>
</tr>
</table>
</form>
</body>