Example of using JavaScript string insertion, deletion, and replacement Functions

Source: Internet
Author: User

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>

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.