- JavaScript:Moving to ES2015
- Ved Antani Simon Timms Narayan Prusty
- 417字
- 2021-07-09 19:07:34
Regular expressions
If you are not familiar with regular expressions, I request you to spend time learning them. Learning and using regular expressions effectively is one of the most rewarding skills that you will gain. During most of the code review sessions, the first thing that I comment on is how a piece of code can be converted to a single line of regular expression (or RegEx). If you study popular JavaScript libraries, you will be surprised to see how ubiquitous RegEx are. Most seasoned engineers rely on RegEx primarily because once you know how to use them, they are concise and easy to test. However, learning RegEx will take a significant amount of effort and time. A regular expression is a way to express a pattern to match strings of text. The expression itself consists of terms and operators that allow us to define these patterns. We'll see what these terms and operators consist of shortly.
In JavaScript, there are two ways to create a regular expression: via a regular expression literal and constructing an instance of a RegExp
object.
For example, if we wanted to create a RegEx that matches the string test exactly, we could use the following RegEx literal:
var pattern = /test/;
RegEx literals are delimited using forward slashes. Alternatively, we could construct a RegExp
instance, passing the RegEx as a string:
var pattern = new RegExp("test");
Both of these formats result in the same RegEx being created in the variable pattern. In addition to the expression itself, there are three flags that can be associated with a RegEx:
i
: This makes the RegEx case-insensitive, so/test/i
matches not onlytest
, but alsoTest
,TEST
,tEsT
, and so on.g
: This matches all the instances of the pattern as opposed to the default of local, which matches the first occurrence only. More on this later.m
: This allows matches across multiple lines that might be obtained from the value of atextarea
element.
These flags are appended to the end of the literal (for example, /test/ig
) or passed in a string as the second parameter to the RegExp
constructor (new RegExp("test", "ig")
).
The following example illustrates the various flags and how they affect the pattern match:
var pattern = /orange/; console.log(pattern.test("orange")); // true var patternIgnoreCase = /orange/i; console.log(patternIgnoreCase.test("Orange")); // true var patternGlobal = /orange/ig; console.log(patternGlobal.test("Orange Juice")); // true
It isn't very exciting if we can just test whether the pattern matches a string. Let's see how we can express more complex patterns.
- HornetQ Messaging Developer’s Guide
- C語(yǔ)言程序設(shè)計(jì)案例教程(第2版)
- ASP.NET Core 5.0開(kāi)發(fā)入門(mén)與實(shí)戰(zhàn)
- Linux C/C++服務(wù)器開(kāi)發(fā)實(shí)踐
- 程序員數(shù)學(xué):用Python學(xué)透線(xiàn)性代數(shù)和微積分
- 軟件測(cè)試工程師面試秘籍
- Cassandra Design Patterns(Second Edition)
- Raspberry Pi 2 Server Essentials
- WebRTC技術(shù)詳解:從0到1構(gòu)建多人視頻會(huì)議系統(tǒng)
- Python從入門(mén)到精通
- JavaScript動(dòng)態(tài)網(wǎng)頁(yè)編程
- 算法設(shè)計(jì)與分析:基于C++編程語(yǔ)言的描述
- 多媒體技術(shù)及應(yīng)用
- 計(jì)算機(jī)組裝與維護(hù)(第二版)
- 程序員的英語(yǔ)