(Important note: uppercase and lowercase alphabet characters are treated as two separate and distinct values, much like ASCII character codes). The expression between the slash symbols (/regex code here/) is an absolute literal. The passed string is then tested for the presence or absence of the search characters - all included characters, appearing in exactly that order with no spaces think of them as a sub-string - and returns a Boolean value: Here is the most basic form of regular expression: one or more straight-up alphanumeric (and/or standard punctuation) characters, between two slashes: / a/. It can be helpful to think of them taking the place of the single quotes (‘ ’) or even double quotes (“”) you would otherwise use to enclose a plain old string. Slashes start and end all regular expressions. It all comes down to the slash (“/”) sign. The cool thing about regex in JavaScript is that regular expressions are actually objects, meaning that we get built-in methods like test() - which returns a Boolean search result indicating presence or absence of matches - and exec() which returns an array of match results (or null if none found).īut that is getting a bit ahead of ourselves. Today we are going to focus on regular expressions in JavaScript. You can even use regex in Google Code Search, not to mention just about any text editor or IDE. Others, like Java, C++, and Python, have regex support as part of their standard library. And this is true for many if not most programming languages: regex is built right into the syntax of some languages, like Perl and JavaScript (courtesy of ECMAScript). So you should care because regular expressions can help you save time while you write shorter, cleaner, and more performant code. And while regexes aren’t the easiest to read, especially for newcomers to the syntax, consider whether you’d rather put in the effort required to puzzle out the logic of one line of cryptic letters and symbols versus the dozens of lines of non-regex code required to achieve the same result. They are fast - faster than the recursive cruft required to not write regex, for sure. This is useful because regular expressions can match just about any pattern. What the Hell Even Is a Regular Expression? And Why Should I Care?Īccording to good ol’ Mozilla Developer Network, “Regular Expressions are patterns used to match character combinations in strings.” Yes, strings as in text or - as the authors of Programming Perl point out - “If you take ‘text’ in the widest possible sense, perhaps 90 percent of what you do is 90 percent text processing.” Specifically, a surprisingly powerful search pattern language that can save any programmer who understands how it works one heck of a lot of time. Slowly, though, it became obvious that those workarounds were more painful than actually buckling down and figuring out what is, after all, just another computer language. I figured there had to be workarounds for any situation that might call for using regular expressions. Also I already felt overloaded just trying to grasp JavaScript and Node.js. I could stumble through the basics when absolutely necessary but avoided actually learning it for real. While on meth).įor the longest time, I was one of those people ridiculously intimidated by regex. All kinds of people, even experienced coders, avoid these freaky-deaky strings of characters that look like chicken tracks (if said chicken happened to be ice skating. Do regular expressions secretly terrify you? Don’t worry, you can admit it - fear of regex is not some shameful quirk you need to keep hidden.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |