Note:
In the following functions, the first two functions retrieve the first and last parts of the query string for other functions. Note: To Call The replaceString (mainStr, searchStr, replaceStr) function once, you can only replace one of the first searchStr strings found in the mainStr string with the replaceStr string, do not replace all the searchStr strings in the mainStr string with the replaceStr string. If you want to replace all the strings, use a loop.
Function source code:
[Code
// Extract all the characters before the search string
Function getFront (mainStr, searchStr ){
FoundOffset = mainStr. indexOf (searchStr );
If (foundOffset =-1 ){
Return null;
}
Return mainStr. substring (0, foundOffset );
}
[/Code]
Copy codeThe Code is as follows:
// Extract all characters after the search string
Function getEnd (mainStr, searchStr ){
FoundOffset = mainStr. indexOf (searchStr );
If (foundOffset =-1 ){
Return null;
}
Return mainStr. substring (foundOffset + searchStr. length, mainStr. length );
}
Copy codeThe Code is as follows:
// Insert the string insertStr before the string searchStr
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 codeThe Code is as follows:
// Delete the estr string
Function deleteString (mainStr, deleteStr ){
Return replaceString (mainStr, deleteStr ,"");
}
Copy codeThe Code is 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;
}
Example:
Assume there is a form for receiving the user's message. We need to replace the text entered by the user in the message content with the HTML tag <br>, and replace the space character, in this way, the message information can be displayed in the original format entered by the user.
The html file is as follows:
Copy codeThe Code is as follows:
<Html>
<Head>
<Script language = "javaScript">
// This is filled by the script function source code provided above, that is, getFront, getEnd, and replaceString.
// [, InsertString, and deleteString].
// Form Detection Function
Function checkForm (form ){
Var gb_contentStr = form. elements ["gb_content"]. value;
// Replace all space characters in the message content
While (gb_contentStr.indexOf ("")! =-1 ){
Gb_contentStr = replaceString (gb_contentStr ,"","");
}
// Replace all carriage return characters in the message with <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 modified message content from the preceding script.
Return true; // submit the message
}
</Script>
</Head>
<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 = "25" align = "right"> message content: </td>
<Td width = "35%" height = "25" align = "center">
<Textarea style = "overflow: auto; width: 100%;" name = "gb_content" rows = "11"> </textarea>
</Td>
</Tr>
</Table>
</Form>
</Body>
</Html>