Js clipboard of the use of clipboarddata.setdata and js match function

  • 2020-03-29 23:51:57
  • OfStack

You often see the effect of clicking a button to copy the contents of an area to the clipboard. In fact, this function is not difficult to implement, the core is the use of a window child object clipboardData method: setData()
Grammar:
      ClipboardData. SetData (sDataFormat sData)

Parameters: SDataFormat: the format of the content to be copied; SData: the content to be copied.

The return value: Successful replication returns true; Failure returns false.


<script language="JavaScript">
function jianqie(id)
{var id;
var text=document.all(id).innerText
if (clipboardData.setData("text",text))
{alert(" Copy success !")}
else
{alert(" Copy the failure !")}
}
</script>

The method of the match function in js is to perform a lookup on a string using the regular expression pattern and return the result containing the lookup as an array. Usage:
StringObj. Match (rgExp)

Where stringObj is the mandatory option. The String object or String literal to which it is looked up.
RgExp is required. Is a regular expression object that contains regular expression patterns and available flags. It can also be a variable name or string literal that contains the regular expression pattern and available flags.

If the match function method in js does not find a match, return null. Returns an array if a match is found and updates the properties of the global RegExp object to reflect the match. The array returned by the match function method in JavaScript has three properties: input, index, and lastIndex. The Input property contains the entire searched string. The Index property contains the position of the matching substring across the searched string. The LastIndex attribute contains the next position of the last character in the last match. If the global flag (g) is not set, the 0 element of the array contains the entire match, and the 1 through n elements contain any child match that ever occurred in the match. This is equivalent to an exec method with no global flag set. If the global flag is set, elements 0 through n contain all matches.

The following example demonstrates the use of the match function method in js:


function MatchDemo(){    
   var r, re;         //Declare variables.      
   var s = "The rain in Spain falls mainly in the plain";    
   re = /ain/i;    //Create a regular expression pattern.      
   r = s.match(re);   //Try to match the search string.      
   return(r);         //Return to the place where "ain" first appeared.      
} 
 

This example illustrates the use of the match function method in js with a g flag setting

function MatchDemo(){    
   var r, re;         //Declare variables.      
   var s = "The rain in Spain falls mainly in the plain";    
   re = /ain/ig;      //Create a regular expression pattern.      
   r = s.match(re);   //Try to match the search string.      
   return(r);         //The returned array contains all the "ain"

The following lines of code illustrate the use of the match function method in js for string literals.

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, similar to indexOf() and lastindexOf(), except that it returns the specified value instead of its position in the string. The indexOf() and lastindexOf() methods return the position number if it cannot find -1. Be case sensitive

<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 articles: