Regular Expression Position Matching
By Stephen Bucaro
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: • Regular Expressions Lookarounds • Easy JavaScript FileReader Code • Replace Drop-down with Drag-and-drop • Java Script to Get Selected Item from Select List Option Group • Java Script Code to Factor Numbers • Easy JavaScript Form Design • Object-Oriented JavaScript • Easy Code to Sort a Table by Column • Regular Expression Basics : Match a Set of Characters • Java Script to Get Selected Item from Drop-down Select List
|