String match() Method - Javascript String

Javascript examples for String:match

Description

The match() method matches a string against a regular expression, and returns the matches, as an Array object.

Parameter Values

Parameter Description
regexpRequired. The value to search for, as a regular expression.

Return Value:

An Array, containing the matches, one item for each match, or null if no match is found

The following code shows how to Search a string for "ain": The result of res will be an array with the values:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from  ww  w.j a va 2s . com*/
    var str = "rain this is a test rain pain plain";
    var res = str.match(/ain/gi);
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

Related Tutorials