JavaScript Regular Expression Tutorial
Regular Expressions
A regular expression is a pattern for characterizing a set of strings. For example, one regular expression might characterize the set of all words that contain at least one pair of consecutive e’s, while another regular expression might characterize the set of all words that begin with a vowel and do not contain the letter k. If a regular expression is the pattern for a particular string, then we say that the string and the regular expression match.
We denote regular expressions by enclosing them within forward slashes. Three examples of regular expressions are /abc/, /y.*er/, and /ing$/. Blank space should not be inserted in a regular expression unless we intend to match blank spaces in a string.
This document introduces the syntax of regular expressions one step at a time, along with examples. We will only consider strings of lower-case letters even though the concept of regular expressions can also handle other characters such as digits and punctuation.
1 A letter stands for itself.
|/squ/ |matches squirrel and esquire and any word containing the substring squ. |
|/bea/ |matches beard but not beta nor abe. The three letters must be consecutive and in the given order. |
|/e/ |matches any word containing an e such as set or here. |
It is important to understand that the regular expression need not represent an entire word as long as it represents some portion of the word.
2 A period stands for any letter.
|/..../ |matches any word of 4 or more letters. |
|/.h.r/ |matches whir, anchor, and toothbrush because each of these three words contains some letter followed by h |
| |followed by some letter followed by r. Note that harp is not a match because some letter is required to |
| |precede the h but that harpsichord is a match because of the substring chor. |
|/.e/ |matches yet, deer, and any word containing an e which is preceded by some letter. Hence eel is matched but |
| |eat is not. |
3 Square brackets stand for a single letter from those enclosed.
|/[aeiou][aeiou]/ |matches words with a pair of consecutive vowels. These vowels may be the same|
| |as in beef or different as in board. Even audio matches though it has two |
| |pairs of consecutive vowels. |
|/[bc][aeiou][bc][aeiou][bc]/ |matches cubic and mycobacteria. |
|/[a-d][r-z]u/ |matches brunt and windsurf because these words contain a letter from the range|
| |a through d, followed by a letter from the range r through z, followed by the |
| |letter u. |
|/[a-cx-z][a-cx-z][a-cx-z]/ |matches cactus, jazz, and sycamore because all three words contain three |
| |consecutive letters taken from either the initial or final three letters of |
| |the alphabet. |
4 If the first character in square brackets is the circumflex ^, then we denote some character not enclosed.
|/[^aeiou][^aeiou][^aeiou]/ |matches words with a triple of consecutive non-vowels such as splash and |
| |kitchen. |
|/[^a-v].[^a-v]/ |matches away, awry, and betwixt which all have a pair of letters taken from the |
| |last 4 of the alphabet surrounding a single letter. |
|/[^e]/ |matches every word except strings consisting entirely of e’s. We might think it |
| |would only match words that do not contain any e’s, but the match is made by the |
| |existence of a single non-e, even if some other letter is an e. |
5 The circumflex ^ at the beginning or the dollar sign $ at the end of a regular expression signify the start or end of a word.
|/^q/ |matches words beginning with q. |
|/[aeiou]$/ |matches words ending with a vowel. |
|/^....$/ |matches words with exactly 4 letters. |
|/^[^aeiou]y/ |matches by, bye, cycle, and any word that begins with a consonant and has y as the second letter. Note |
| |that this example illustrates two distinct meanings of ^ depending on whether it appears as the first |
| |character of the regular expression or as the first character inside square brackets. These are the |
| |only two places it may appear. |
6
The * means zero or more of the previous character or regular expression.
The + means one or more of the previous character or regular expression.
The ? means zero or one of the previous character or regular expression.
|/pe*p/ |matches kappa, peptide, peep, and any word with a pair of p’s separated by zero or more e’s. |
|/pe+p/ |matches peptide and peep, but not kappa because one or more e is required to separate a pair |
| |of p’s. |
|/pe?p/ |matches kappa and peptide, but not peep because there must be a pair of p’s with either 0 or 1|
| |e separating them. |
|/e.*e/ |matches any word with at least two e’s, which may or may not be adjacent. |
|/e.+e/ |matches any word containing a pair of non-adjacent e’s, signified in the regular expression by|
| |the presence of one or more letters between the e’s. Note that evergreen and decree are |
| |matches despite their adjacent e’s because each also contains a pair of non-adjacent e’s. |
The * is a very powerful element of the regular expression language. It is often used as part of .* to mean that there may or may not be some sequence of letters. But it is important to realize that placing the * allows for the possibility of the empty string. Consider the example of all words that contain a pair of adjacent vowels that are not also part of a substring of 3 consecutive vowels. For example we want to include words like eel and audio while excluding words like dog and beautiful. As a first attempt we might try the regular expression
/[^aeiou][aeiou][aeiou][^aeiou]/
This captures all words that have a pair of adjacent vowels surrounded by non-vowels. The problem is that this regular expression excludes words such as radio whose adjacent vowel pair falls at the beginning or end of the word because the regular expression is requiring that there be letters before and after the vowel pair. While we don’t yet have the tools to correct this problem, the following is NOT an adequate correction.
/[^aeiou]*[aeiou][aeiou][^aeiou]*/
This regular expression would still match the word beauty because [^aeiou]* would match the b, [aeiou][aeiou] would match the adjacent pair ea, and [^aeiou]* would match the empty string between the letters a and u of beauty.
7 Curly braces cause the preceding letter or regular expression to be matched a number of times.
{n} match n times
{m,n} match at least m and no more than n times
{n,} match at least n times (note there is no space after the comma)
|/e{2}/ |matches words with consecutive e’s |
|/[aeiou]{3}/ |matches words with three vowels in a row. |
|/e[^aeiou]{5,}e/ |matches emphysema, a word with a pair of e’s separated by at least 5 letters, all of which are |
| |consonants. |
Note that * is equivalent to {0,}, that + is equivalent to {1,}, and that ? is equivalent to {0,1}.
8 Parentheses group sub-expressions as a unit for applying *, +, ?, and {}.
|/(et)+/ |matches poet and dietetic because they have one or more repetitions of the sub-expression et. |
|/(.e){4}/ |matches dereference and telemeter because they have 4 consecutive repetitions of the |
| |sub-expression consisting of a character followed by an e. We do not match exegete because |
| |there is no character before the initial e. |
|/([aeiou]{2}.*){3}/ |matches audiovisual, bourgeoisie, and questionnaire because they contain 3 repetitions of the |
| |subexpression consisting of doubled vowels followed by 0 or more letters. We can see this by |
| |grouping the matched words as follows: (aud)(iovis)(ual), b(ourg)(eois)(ie), and |
| |q(uest)(ionn)(aire). |
9 The symbol | means “or”.
|/(ee|aa)r/ |matches bazaar and career because each has an r preceded by either ee or aa. |
10. The characters \b stand for a word boundary.
We return to the previous example of all words that contain a pair of adjacent vowels that are not also part of a substring of 3 consecutive vowels. Earlier we saw that
/[^aeiou][aeiou][aeiou][^aeiou]/
did not match radio because of the requirement that a letter follow the vowel pair. We now have a means of matching a pair of vowels that begin the word, end the word, or are surrounded by consonants.
/(\b|[^aeiou])[aeiou][aeiou](\b|[^aeiou])/
Implementing Regular Expressions in JavaScript
A regular expression can be specified as a literal.
var reg = /e./;
Another method is to enter the regular expression as a String and then convert it to a regular expression.
var reg_string = “e.”;
var reg = RegExp( reg_string );
Here is a means of taking in a regular expression from the user as a String returned from the prompt command. Note that the user should enter only the regular expression without the forward slashes / /.
var user_reg = prompt(“Please enter a regular expression.”,””);
var reg = RegExp( user_reg );
To test if a regular expression matches a String, we use the regular expression method .test() and pass it a String. The method returns true for matches.
Here is the code for taking in a user’s regular expression and searching an Array called words to output all matches.
var user_reg = prompt(“Please enter a regular expression.”,””);
var reg = RegExp( user_reg );
for(i = 0; i < words[i].length; i++)
if( reg.test( words[i] ) )
document.write( words[i] + “” );
To declare a regular expression such as /..a../ to be case insensitive, use one of the following.
var reg = /..a../i;
reg = RegExp(“..a..”, “i”);
Exercises
Do not consider y as a vowel.
1. Find words that contain, but do not begin with, squ.
2. Find words with more than one q.
3. Find words with at least three occurrences of er.
4. Find words containing but not ending in ing.
5. Find words with a pair of consecutive triples of vowels.
6. Find words divisible into two non-empty parts, an initial part of letters from a through m followed by a part of letters from n through z.
7. Find words that have a q not followed by u.
8. Find words with exactly 3 e’s.
9. Find words that alternate between vowels and consonants.
10. Find words that do not begin with y, but which contain y before any other vowel.
11. Find words that contain y and no other vowels.
12. Find words that begin with at least 4 initial consonants.
13. Find words that are not capitalized.
14. Find words that have 6 initial non-vowels.
15. Find words with at least 5 e’s.
16. Find words with at least 20 letters.
17. Find words that have 3 y’s.
18. Find words that end in ism.
19. Find 6 letter words with e’s in the even positions.
20. Find words that start and end with a vowel, but have no vowels in between.
21. Find words that have both sh and ch consonant clusters.
22. Find words that would be typed only using the left hand.
23. Find words that would be typed only using the right hand.
24. Find words that would be typed by alternating left and right hands.
25. Find 4 letter words with vowels in all but the second position.
26. Find words with 9 or more vowels.
27. Find words that begin with a vowel pair and alternate between vowel and non-vowel pairs.
28. Find words with 4 consecutive vowels.
29. Find 3 letters words with no vowels. (Probably abbreviations)
30. Find words that end in g but not in ing.
Answers to Exercises
1. /.squ/
2. /q.*q/
3. /er.*er.*er/
4. /ing./
5. /[aeiou]{3}.*[aeiou]{3}/
6. /^[a-m]+[n-z]+$/
7. /q([^u]|\b)/
8. /^[^e](e[^e]*){3}$/
9. /^[aeiou]?([^aeiou][aeiou])*[^aeiou]?$/
10. /^[^aeiouy]+y/
11. /^[^aeiouy]*(y[^aeiouy]*)+$/
12. /^[^aeiouy]{4}/
13. /^[^A-Z]/
14. /[^aeiou]{6}/i
15. /(e.*){5}/
16. /.{20}/
17. /(y.*){3}/
18. /ism$/
19. /^(.e){3}$/
20. /^[aeiou][^aeiou]*[aeiou]$/
21. /(sh.*ch)|(ch.*sh)/
22. /^[a-gq-tv-xz]+$/
23. /^[h-puy]+$/
24. /^[a-gq-tv-xz]?([h-puy][a-gq-tv-xz])*[h-puy]?$/
25. /^[aeiou][^aeiou][aeiou][aeiou]$/
26. /([aeiou].*){9}/
27. /^([aeiou][aeiou][^aeiou][^aeiou])+$/
28. /[aeiou]{4}/
29. /^[^aeiou]{3}$/
30. /((\b)|(\b.)|(^i][^n])|([^i].)|(.[^n]))g$/
Advanced Regular Expressions
Capturing Parentheses
When part of a regular expression is enclosed in parentheses, the regular expression remembers the enclosed portion and can reference it later in the expression by a number. For example, here is the pattern for all words that start and end with the same letter.
/^([a-z]).*\1$
The first set of parentheses is matched exactly by the \1. Here is the regular expression for all 4 letter palindromes.
/^([a-z])([a-z])\2\1$/
Here is the regular expression for all palindromes having no more than 18 letters:
/^([a-z]?)([a-z]?)([a-z]?)([a-z]?)([a-z]?)([a-z]?)([a-z]?)([a-z]?)
([a-z])\9?\8\7\6\5\4\3\2\1$/
Match if followed by
The regular expression X(?=Y) matches X if it is followed by Y, while X(?!Y) matches X if it is not followed by Y.
To find all words that follow the rule “i before e except after c, use /c(?=ei)/. Two exceptions to the rule would be /c(?ie)/ and /[^c]ei/.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- cover letter elsevier
- home ieee industry applications society
- some phonological constraints in tagalog
- nomenclature home ieee power and energy society
- world academy of research in science and engineering
- professional letter
- mrs cholkar s mathematics class announcements
- javascript regular expression tutorial
Related searches
- javascript regular expression match
- regular expression in java
- regular expression in java tutorial
- java regular expression example
- regular expression interactive tutorial
- javascript regular expression replace
- regular expression blank
- regular expression remove blank lines
- regular expression case insensitive match
- regular expression empty line
- regular expression special character
- regular expression remove special characters