Match string using regular expressions in JavaScript

Description

The following code shows how to match string using regular expressions.

Example


<!--from  w  w w.  j a  v  a  2s  .  c  o  m-->

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

var text = "look, room, doom";
var pattern = /.oo/;

var matches = text.match(pattern);
document.writeln(matches.index); //0
document.writeln(matches[0]); //"oo"
document.writeln(pattern.lastIndex); //0

</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Match string using regular expressions in JavaScript
Home »
  Javascript Tutorial »
    Data Type »
      String
...
Convert string to lower case with toLowerCa...
Convert string to upper case based on local...
Convert string to upper case in JavaScript
Convert undefined to string with String() i...
Convert value to String with toString metho...
Count the words in JavaScript
Create String from null in JavaScript
Create String object by calling its constru...
Demonstrate that all negative numbers are c...
Divide string into several parts with Strin...
Escape a string with escape function in Jav...
Find all occurrences for the sub string wit...
Get sub string with String substr() method ...
Get sub string with string substring method...
Get substring from the end of a string in J...
Get substring from the end with string subs...
Get substring with String slice() method in...
Get the character in the given position for...
Get the character's character code at the g...
Get the length of a string in JavaScript
Get the number of characters in the string ...
Match string using regular expressions in J...
Parse string in scientific to number in Jav...
Repalce a string with regular expression in...
Replace a substring with String in JavaScri...
Replace string with a custom function in Ja...
Replace string with regular-expression in J...
Reverse string using substr in JavaScript
Reverse the words in JavaScript
Search a string from the middle of another ...
Search a string using regular expressions i...
Search a sub string with String indexOf() i...
Search from the end of a string with String...
Start sub string backwards from the middle ...
Trim white spaces from a String in JavaScri...
Unescape escaped string in JavaScript
Use indexOf to validate an email address in...
Use replace() method to replace letters in ...
...