May 5th, 2019Using Regular Expressions in JavaScript [Part 2]
![]() |
Using Regular Expressions in JavaScript [Part 2]
Introduction
Regular expressions is a powerful tool for the incoming data processing. The problem demanding replacement or text search, can be successfully solved by this “language inside language”. And thought maximum effect from the regular expressions might be achieved by using server languages, it is not no point to undervalue the opportunities of this client’s utility.
Basic concepts
Regular expression is a tool for lines or symbols strings processing, which determines text template.
Modifier is designed for regular expression “supervision”.
Metacharacters are special symbols, which serve as commands of regular expressions language.
Regular expression is worked out as usual variable, just it is used slash instead of quotes, e.g.:var reg=/reg_expression/
Simple templates are such templates, which do not need any special symbols.
Let us say, our task is to change all the letters “p” (both capital and lowercase) to Latin letter “R” in phrase Regular expressions.
We create template var reg=/p/ and realize it with replace method
<script language=”JavaScript”>
var str=”Regular expressions”
var reg=/r/
var result=str.replace(reg, “R”)
document.write(result)
</script>
As a result we receive <<RegulaR expressions>>, change was made only in first occurrence of letter “r” according to match-case.
But this result doesn’t suit under conditions of our task… Here we need modifiers “g” and “i”, which may be used both separately and altogether. These modifiers are put inside the template of regular expression, after slash. They have the following values:
modifier “g” specifies line search as “global”, that is to say in our case change happens with all the occurrences of letter “p”. Now the template looks this way: var reg=/r/g. If we insert it into our code:
<script language=”JavaScript”>
var str=”Regular expressions”
var reg=/r/g
var result=str.replace(reg, “R”)
document.write(result)
</script>
So we receive <<RegulaR expRetions>>.
modifier “i” specifies case-insensitive line search, so if we add this modifier into our template var reg=/r/gi, after script processing we receive required result of our task:<<regular expretions>>.
Special symbols (Metacharacters)
Metacharacters specify symbols type of the required line, type of external environment of line in text, as well as quantity of the symbols of separate type in browseable text. That is why it is possible to divide metacharacters into three groups:
- Metacharacters of coincidence search.
- Quantified metacharacters.
- Metacharacters of position.
-
Metacharacters of coincidence search:
\b word board, which specifies condition, under which the template is to be processed at the end or at the beginning of the words.
\B not word board, which specifies condition, under which the template is not processed at the end or at the beginning of the words.
\d digit from 0 to 9.
\D not a digit.
\s single empty symbol, corresponding to space symbol.
\S single nonempty symbol, any symbol other than space.
\w digit, letter of flatworm.
\W not digit, letter of flatworm.
. any symbol, any signs, letter, digits, etc.
[ ] symbol series, specifies condition, under which template has to be processed under any symbols coincidence put into square brackets.
[^ ], set of non-occurransing symbols, specifies condition, under which template has not to be processed under any symbols coincidence put into square brackets.
Quantified metacharacters:
* Zero or more times.
? Zero or one time
+ One or more times.
{n} exactly n times.
{n,} n or more times.
{n,m} at least n times but less than m times.
Metacharacters of position:
^ at the line beginning.
$ at the end of line.
Some methods of work with templates
replace — this method we had already used at the beginning of the article, it is assigned for the sample search and change of found subchain to new subchain.
test — this methods checks out whether coincidence in the line takes place towards the template, and retrieves false, if comparison with the sample failed, otherwise true.
For example:
<script language=”JavaScript”>
var str=”JavaScript”
var reg=/PHP/
var result=reg.test(str)
document.write(result)
</script>As a result displays false, as the line “JavaScript” is not equal to line “PHP“.
Also method test may retrieve any other line assigned by the programmer to true or false.
For example:
<script language=”JavaScript”>
var str=”JavaScript”
var reg=/PHP/
var result=reg.test(str) ? “Line coincided”: “Line did not coincide”
document.write(result)
</script>In this case a result will be displayed as “Line did not coincide”.
exec — this method processes matching of the line with the sample, assigned by the template. If correlation with the sample failed, the meaning null is retrieved. Otherwise the result is subchains array, corresponding to specified sample. /*The first element of the array will be equal to initial line complying with the specified template*/
for example:
<script language=”JavaScript”>
var reg=/(\d+).(\d+).(\d+)/
var arr=reg.exec(”I was born on 15.09.1980″)
document.write(”Date of birth: “, arr[[ ,]] “< br>”)
document.write(”Day of birth: “, arr, “< br>”)
document.write(”Month of birth: “, arr, “< br>”)
document.write(”Year of birth: “, arr, “< br>”)
</script>As a result we receive four lines:
Date of birth: 15.09.1980
Day of birth: 15
Month of birth: 09
Year of birth: 1980Conclusion
Far not all the abilities and advantages of the regular expressions are described in this article.
For deeper studying of this matter I’d recommend you to learn RebExp object.
Also I’d like to point out that syntax of regular expressions is the same in JavaScript and PHP.
For example if you need to check out the correctness of e-mail input, the regular expression looks the same
way for JavaScript and PHP: /[0-9a-z_]+@[0-9a-z_^.]+.[a-z]{2,3}/i.