/

HTML / CSS / JavaScript Tutorial

JavaScript regular expressions: [xyz] find pattern

[this page | pdf | back links]

A JavaScript regular expression takes the form: /pattern/modifiers

 

Regular expressions can also include brackets to accept (or reject) a range of characters. An expression containing [xyz] means find any character within the bracket.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
var str = "Hello world";
document.getElementById("Example").innerHTML =
  'var str = "Hello world";<br>' +
  'var x = str.replace(/[a-f]/g,"*");';
document.getElementById("Result").innerHTML
  = str.replace(/[a-f]/g,"*");
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Regular Expressions


Desktop view | Switch to Mobile