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 Position Matching

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 characters in specific positions within a string. The example below will match the word "All" at the beginning of the string "All men are created equal".

<script>
let strTarget = "All men are created equal"; 
let regexp = /^All/; 
var res = regexp.test(strTarget); 
alert(res);
</script>

The caret ^ character in the regular expression the script above instructs the matching to be at the beginning of the string. Upon execution, the script will display a message box with the message "true", indicating that the word "All" was matched at the beginning of the string.

<script>
let strTarget = "Equal created are All men"; 
let regexp = /^All/; 
var res = regexp.test(strTarget); 
alert(res);
</script>

In the script shown above, the string has been changed to "Equal created are All men". Upon execution, the script will display the message "false", indicating that the word "All" was NOT matched at the beginning of the string.

The example below will match the word "equal" at the end of the string "All men are created equal".

<script>
let strTarget = "All men are created equal"; 
let regexp = /equal$/; 
var res = regexp.test(strTarget); 
alert(res);
</script>

The dollar $ character in the regular expression the script above instructs the matching to be at the end of the string. The script will display a message "true", indicating that the word "equal" was matched at the end of the string.

A regular expression can match both beginning and ending characters, disregarding any characters in the middle of the sentence. The example below will match sentences that have the word "All" at the beginning, and "equal" at the end.

<script>
let strTarget = "All men are created equal"; 
let regexp = /^All.*equal$/; 
var res = regexp.test(strTarget); 
alert(res);
</script>

The caret ^ character instructs the matching to be at the beginning of the string. The .* instructs the matching to match any character, repeated 0 or more times". The dollar $ character instructs the matching to be at the end of the string.

The example below will match the word "men" in the position 4 of of the string. Remember that positions in a string begin with number 0.

<script>
let strTarget = "All men are created equal"; 
let regexp = /^.{4}(men)/; 
var res = regexp.test(strTarget); 
alert(res);
</script>

The caret ^ character instructs the matching to be at the beginning of the string. The .{4} instructs the matching to match any character, repeated 4 times", then match the word "men".

In this article you learned how to use regular expressions to match characters at the beginning, end, both beginning and end, or at any specified location in a string.

More Java Script Code:
• JavaScript Code to Display Wait Cursor While Image Loads
• HTML5 Canvas lineCap and lineJoin Attributes
• JavaScript Variable Scope
• Introduction to HTML5 Canvas
• Easy Slide Show Code With Linked Slides
• Calendars for Your Website
• Make Your Own Graphical Digital Clock
• Let Your Web site Visitors Set Font Size
• JavaScript to Copy and Paste Text to the Clipboard
• Java Script Comparison Operators

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