Extra Features in ECMAScript String Regular Expressions
ECMAScript String Regular Expressions – Part 8
Forward: What we have learned would solve many of our problems. However, there will come a time when you would want to do more in regex. So this part of the series is to give you those extra features.
By: Chrysanthus Date Published: 26 Jul 2012
Introduction
Sub String followed by another
The official way of writing ECMAScript is “ECMAScript”. You have the word “ECMA” followed immediately by “Script”. In ECMAScript, it is possible for you to match one sub string if it is immediately followed by another. The syntax is:
x(?=y)
Here, ‘x’ and ‘y’ are sub strings. This is to match ‘x’ if it is immediately followed by ‘y’. So, you can match “ECMA” if it is immediately followed by “Script”. Consider the following subject:
var subject = "I like the ECMAScript language."
The regex to match “ECMA” immediately followed by “Script” is
/ECMA(?=Script)/
The following code illustrates this and produces a match.
<script type="text/ECMAScript">
var subject = "I like the ECMAScript language."
if (subject.search(/ECMA(?=Script)/) != -1)
alert('Matched');
else
alert('Not Matched');
</script>
Sub String Not followed by another
Now, ECMA is a computer language; ECMAScript is also a computer language. You may be looking for ECMA and not ECMAScript. The syntax for this is:
x(?!y)
Here, ‘x’ and ‘y’ are sub strings. This is to match ‘x’ if it is not immediately followed by ‘y’. So, you can match “ECMA” if it is not immediately followed by “Script”. Consider the following subject:
var subject = "I like the ECMA language. It is a Scripting language."
The regex to match “ECMA” not immediately followed by “Script” is
/ECMA(?!Script)/
The following code illustrates this and produces a match:
<script type="text/ECMAScript">
var subject = "I like the ECMA language. It is a Scripting language."
if (subject.search(/ECMA(?!Script)/) != -1)
alert('Matched');
else
alert('Not Matched');
</script>
In part 4 of this series, I explained what grouping means. You group a part of a regex using parentheses. You can capture (remember) what is in a group if you use the string match() method and assigns the returned item to the a variable. We saw this:
var arr = "This is one and that is two.".match(/(one).*(two)/);
It is possible for you to group part of a regex to have the benefit of its entity behavior without capturing (remembering) it. The syntax for this is:
(?:x)
If you do not want the group (one) above to be captured, use:
(?:one)
The following code illustrates this:
<html>
<head>
</head>
<body>
<script type="text/ECMAScript">
var arr = "This is one and that is two.".match(/(?:one).*(two)/);
alert(arr[0]);
alert(arr[1]);
alert(arr[2]);
</script>
</body>
</html>
The output is:
one and that is two
two
undefined
As you can see from the output, the group (?:one) is not captured, because of “?:” that precedes “one”.
That is it for this part of the series.
Conclusion
In this series, we have learned Regular Expressions in ECMAScript, using some string object methods. The methods we have used are the search(), match(), replace() and split() methods. This is the end of the series. I hope you appreciated it.
Chrys
Related Links
Major in Website DesignWeb Development Course
HTML Course
CSS Course
ECMAScript Course
NEXT