Home > Web Front-end > JS Tutorial > Introduction to the usage of js clipboard (clipboardData.setData) and js match function_javascript skills

Introduction to the usage of js clipboard (clipboardData.setData) and js match function_javascript skills

WBOY
Release: 2016-05-16 17:14:10
Original
1523 people have browsed it

I often see this effect: clicking a button copies the content in a certain area to the clipboard. In fact, this function is not difficult to implement. The core is to use a method of the window sub-object clipboardData: setData()
Syntax:
clipboardData.setData(sDataFormat, sData)

Parameters: sDataFormat: the format of the content to be copied; sData: the content to be copied.

Return value: Returns true if the copy is successful; returns false if it fails.

Copy code The code is as follows:



The match function method in js is Performs a search on a string using a regular expression pattern and returns the results containing the search as an array. Usage:
stringObj.match(rgExp)

where stringObj is required. The String object or string literal to search for.
rgExp is required. Is a regular expression object containing the regular expression pattern and available flags. Can also be a variable name or string literal containing a regular expression pattern and available flags.

If the match function method in js does not find a match, return null. If a match is found an array is returned and the properties of the global RegExp object are updated to reflect the match. The array returned by the match function method in JavaScript has three attributes: input, index and lastIndex. The Input property contains the entire searched string. The Index property contains the position of the matching substring within the entire string being searched for. The LastIndex property contains the position next to the last character in the last match. If the global flag (g) is not set, element 0 of the array contains the entire match, and elements 1 to n contain any submatches that occurred in the match. This is equivalent to the exec method without setting the global flag. If the global flag is set, elements 0 to n contain all matches.

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

Copy the code The code is as follows:

function MatchDemo(){
var r, re; // Declare variables.                                                                                                    
r = s.match(re); // Try to match the search string.
return(r); // Return to the first occurrence of "ain".
}


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


function MatchDemo(){                                                                                                                                                                                                                                        .                                                                                                                                                                                                                                 .
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 use of the match function method in js for string literals.



Copy code
The 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 the string. This method is similar to indexOf() and lastindexOf(), the difference is that it returns the specified value rather than the position of the specified value in the string. The indexOf() and lastindexOf() methods return the position number and return -1 if not found. Note that it is case sensitive



Copy the code
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template