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

What is a Regular Expression?

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. Regular expressions are very powerful, but also difficult to understand and complicated to use. But sometimes a regular expression is a the most optimal, or the only solution for a task.

You can use regular expression with almost all programming languages: JavaScript, Java, VB, C, C++, C#, Python, and many others. They work similar in most language with slight syntax variations and different support for advanced features in different languages. Since JavaScript is by far the most popular programming language, and because JavaScript is an interpreted language, that's the language I will use here.

The first characters of a regular expression are the delimiters. The delimiters mark the boundaries of the regular expression pattern. JavaScript uses the forward slash (/) as a delimiter. An example of a most basic regular expression is shown below.


The four letters inside the regular expression delimiters are character literals. Shown below is an example of how this regular expression could be used.

var strTarget = "abcdefghijkl";
var loc = strTarget.search(/efgh/);

In case you are not familiar with how to execute code like that shown above. 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 shown above 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.

When you execute the above code, a message box will open displaying the number 4. The code creates a text string "abcdefghijkl" and then passes the regular expression /efgh/ to the JavaScript String Object method search. The search method returns the number 4, storing it in the variable loc, which is the character position in the target string where it found the regular expression literal efgh.

The code then uses the JavaScript alert method to display the contents of the variable loc. 4 is the 0 based location of the first character of efgh in the string "abcdefghijkl". Had the search the method not found the literal efgh in the target string, it would have returned -1.

Now I know many of you already know how to execute code like that shown above. I don't mean to insult your intelligence. But I need to explain the above because the this is the method I will use to in future articles in this series, and I promise you things will get more advanced in future articles. Way more advanced.

More Java Script Code:
• Make Your Own Graphical Digital Clock
• Easy Slide Show Code With Linked Slides
• Code for Java Script Cylinder / Cone Volume Calculator
• JavaScript to Replace All Occurrences of a Substring Within a String
• Synchronous and Asynchronous Script Loading
• Regular Expressions Lookarounds
• JavaScript Code to Display Wait Cursor While Image Loads
• How to Disable the Browser Back Button
• JavaScript Code to Save and Load a Table Using HTML5 Web Storage
• Easy Slide Show Code

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-2021 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268