Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Regular Expression Basics : Match a Set of Characters

A Regular Expression (regex) is a sequence of characters that define a pattern that allows you to search, match, locate, replace, manipulate, and manage text. I explained the basics of Regular Expressions in an earlier article: What is a Regular Expression?

In case you are not familiar with how to execute code like that in this article, you open a new text file (use a basic ASCII text editor like Windows Notepad, not a word processor. Word processors add formatting characters to the text). Paste the code into the text file. Save the text file with a name that has the file extension .htm. (say test.htm) Double-click on the file to open it in your web browser.

In this article you'll learn how to use regular expressions to match a set of of characters. The example below will match uppercase A-Z characters in the string "We hold these Truths to be self-evident, that All Men are Created Equal..."

<script>
let strTarget = "We hold these Truths to be self-evident, that All Men are Created Equal...";
let regexp = /[A-Z]/g;

let result = strTarget.match(regexp);
alert(result);
</script>

To match a set of of characters, create a "character set" or "character class". Place the characters you want to match between square brackets. You can use a hyphen inside a character class to specify a range of characters, for example [A-Z].

In the example above, I store the regular expression /[A-Z]/g in a variable named regexp. I pass that variable to the JavaScript String object match method. The g specifier causes the regular expression to search for matches through the entire string.

The String object match method stores the returned matches in a variable named result. If there are no matches, the match method will return null. I use the alert method to display the content of the variable result.

This causes the String match method to return W,T,A,M,C,E.

In previous examples, I used the keyword var to declare variables. Assuming that you will be programming for newer browser versions, the keyword let would be a better choice to declare variables. The difference between var and let is that var is function scoped and let is block scoped. This means that let provides much tighter scoping. (in programming a varaibles scope is the part of the code in which it will be visible).

Also you can actually use variables in JavaScript without declaring them first, which is bad programming practice, while variables declared with let are not accessible before they are declared.

In the example below, the regular expression /[aeiou]/g causes the String match method to return all lowercase vowels.

<script>
let strTarget = "We hold these Truths to be self-evident, that All Men are Created Equal ...";
let regexp = /[aeiou]/g;

let result = strTarget.match(regexp);
alert(result);
</script>

This causes the String match method to return e,o,e,e,u,o,e,e,e,i,e,a,e,a,e,e,a,e,u,a.

In the example below, the regular expression /[A-Ma-m]/ will match all uppercase or lowercase letters from the first half of the alphabet.

<script>
let strTarget = "We hold these Truths to be self-evident, that All Men are Created Equal ...";
let regexp = /[A-Ma-m]/g;

let result = strTarget.match(regexp);
alert(result);
</script>

This causes the String match method to return e,h,l,d,h,e,e,h,b,e,e,l,f,e,i,d,e,h,a,A,l,l,M,e,a,e,C,e,a,e,d,E,a,l.

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro



Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2019 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268