Announcement

Collapse
No announcement yet.

Ecmascript String Regular Expressions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Ecmascript String Regular Expressions

    Introduction
    The best way to explain the meaning of regular expression is by illustration. In this article I introduce you to ECMAScript regular expressions, achieved with the string object. In order to understand ECMAScript regular expressions, you need to have basic knowledge in ECMAScript, including ECMAScript objects.

    Searching a Word in a String
    Consider the following string variable:

    var subject = "The boys are on the way to the college.";

    A program or HTML document can have many strings. In fact the whole HTML document can be considered as one very long string. For now, let us focus on the string above. You may not know the content of the string (see reasons below). While the program is running (or web page is displayed) you may want to know if the string has the word, "boys". In this case, you would fist type what is known as a regular expression and assign it to a variable. So you would type,

    var regexp = /boys/;

    Now, the following ECMAScript statement (right operand) returns a positive integer, if the word, "boys" is found in the string or -1 if "boys" is not found in the string:

    var pos = subject.search(regexp);

    Whatever is returned is held by the variable, pos. In the right operand of the statement, subject, is the string code object, that has the word we are looking for, while, search() is a method of the object. It is this method that actually returns a positive integer or -1. In the statement, there is a dot between the string object and the method.

    The string, which has the word we are looking for is called, the Subject. What we are looking for, placed in between two forward slashes (including the forward slashes), such as with, /boys/, is called, the Regular Expression, abbreviated, regexp.

    Actually, the phrase, "Regular Expression" is vague in meaning: It can mean the topic or it can mean the two forward slashes and their content. However, as you begin and continue with the tutorial series, whose link I give below, the use of the phrase will not be a problem.

    A More Involving Example
    The above example is simple and you do not even need regular expression techniques to determine whether a subject has the word. In other words, you can use string object methods to find out whether a string (subject) has a particular word.

    What about the case, where you would want to check if the above subject has either the word, "boys" or the word, "girls". In this case, the regexp variable statement would be,

    var regexp = /boys|girls/;

    The | symbol in the regexp value, means that "boys" or "girls" would be looked for in the subject string. This regexp is more complicated than the previous one, which was just, /boys/.

    For this regexp, you can have the following ECMAScript code segment:

    var subject = "The boys are on the way to the college.";
    var regexp = /boys|girls/;
    var pos = subject.search(regexp);

    In this code, the search() method returns a positive integer, indicating that "boys" or "girls" was found, in the subject string.

    Subjects in a Web Page
    ECMAScript is a language used to produce interactivity in a web page. So, where in the web page would you have strings that will act as subjects for the topic, ECMAScript Regular Expressions? One source, is from the Input Text control. Another source is from the Text Area input control. These two sources are HTML Form elements.

    An HTML document (web page) consists of text. The images, sound, and video that you get in a web page are files that arrive at the document because certain HTML elements in the document download them. So another source of text for the subject is from each element of the document. However the text of an HTML element is not very confined. Do not worry about that for now. When you complete the series, whose hyperlink is given below, you will be able to handle the confining issue.

    Another source of text, for the subject string, is the whole HTML document text. This one is confined.

    Well, I have introduced above, ECMAScript String Regular Expressions. There are more precisions to make and more to learn. I have already prepared the tutorial series that will take you through the learning process. The series has been written in a step-by-step fashion, well formatted (typed), with no special character missing. Click the following link to start the series:

    Ecmascript String Regular Expressions

    Chrys
Working...
X