Introduction to js clipboard usage (clipboardData. setData) and js match Functions

Source: Internet
Author: User

We often see this effect: click a button to copy the content in a region to the clipboard. In fact, this function is not difficult to implement. The core is to use the clipboardData method of the window sub-object: setData ()
Syntax:
ClipboardData. setData (sDataFormat, sData)

Parameters:SDataFormat: Format of the content to be copied; sData: content to be copied.

Return Value:True is returned for successful replication; false is returned for failed replication.
Copy codeThe Code is as follows:
<Script language = "JavaScript">
Function jianqie (id)
{Var id;
Var text = document. all (id). innerText
If (clipboardData. setData ("text", text ))
{Alert ("Copied successfully! ")}
Else
{Alert ("copying failed! ")}
}
</Script>

In js, the match function uses the regular expression mode to perform a search for a string and returns the result containing the search as an array. Usage:
StringObj. match (rgExp)

StringObj is required. String object or String text to search.
RgExp is required. It is a regular expression object that contains the regular expression mode and available flag. It can also be a variable name or string text that contains the regular expression mode and available signs.

If no match is found in the match Function Method in js, null is returned. If a match is found, an array is returned and the attributes of the global RegExp object are updated to reflect the matching result. The array returned by the match function in JavaScript has three attributes: input, index, and lastIndex. The Input attribute contains the entire searched string. The Index attribute contains the position of the matched substring in the entire searched string. The LastIndex attribute contains the next position of the last character in the last match. If the global sign (g) is not set, the 0 element of the array contains the entire match, and the 1st to n element contains any child match that has occurred during the match. This is equivalent to the exec method without a global flag. If a global flag is set, elements 0 to n contain all matches.

The following example demonstrates the usage of the match function in js:
Copy codeThe Code is as follows:
Function MatchDemo (){
Var r, re; // declare the variable.
Var s = "The rain in Spain falls mainly in the plain ";
Re =/ain/I; // create the regular expression mode.
R = s. match (re); // try to match the search string.
Return (r); // return the place where "ain" appears for the first time.
}

This example describes how to use the match function in js with g flag settings.
Copy codeThe Code is as follows:
Function MatchDemo (){
Var r, re; // declare the variable.
Var s = "The rain in Spain falls mainly in the plain ";
Re =/ain/ig; // create the regular expression mode.
R = s. match (re); // try to match the search string.
Return (r); // The returned array contains all "ain"

The following lines of code demonstrate the usage of the match function in the string text js.
Copy codeThe Code is as follows:
Var r, re = "Spain ";
R = "The rain in Spain". replace (re, "Canada ");

The match () method is used to find the specified value from a string. This method is similar to indexOf () and lastindexOf (). The difference is that it returns the specified value, instead of specifying the position in the string. The indexOf () and lastindexOf () Methods return the position number. If no value is found,-1 is returned. Case Sensitive
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var str = "Hello world! "
Document. write (str. match ("world") + "")
Document. write (str. match ("World") + "")
Document. write (str. match ("worlld") + "")
Document. write (str. match ("world! "))
</Script>

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.