![regex caret regex caret](https://ak1.ostkcdn.com/images/products/is/images/direct/dfd0af94fb0e9401f4c186b11bf470abee439b65/24-Pack-Assorted-Color-Plastic-Regular-Mouth-Mason-Jar-Lids-Top-Lid-Food-Storage-Caps-for-Mason-Canning-Balls-Jars.jpg)
![regex caret regex caret](https://i.stack.imgur.com/qXDLz.png)
This is convenient: When you want to match some characters at the end of a string, you don't have to worry about whether it might have a line break at the end.įor all engines except PCRE, the final line break character must be a linefeed character \n. For instance, in the apple\n, e$ matches the final e. In all major engines except JavaScript, if the string has one final line break, the $ anchor can match there. If you add a line break at the end of your the apple string, as in the apple\n, the JavaScript engine no longer matches the final e.Īll Engines Except JavaScript: Match Before One Final Line Break JavaScript, however, does not have this kind of flexibility (which won't surprise you if you remember that JavaScript is by a long shot the worst among all the major regex engines). For instance, if your string is the apple, the $ anchor matches the position after the e, and e$ matches the final e, but not the first.įor convenience, most engines build some flexibility into $, allowing it to match before final line breaks-we'll examine this in the following sections. Let's start with the narrowest meaning then gradually expand.Īll Engines: Match at the Very End of the StringĪll engines agree on one string position where the dollar anchor $ is allowed to match: at the very end of the string. The dollar anchor changes its meaning depending on which engine we use and on whether the multiline mode is on. When that mode is set, ^a matches the a on both of these lines:
Regex caret how to#
That mode is often turned on with an inline modifier (?m), but this varies across engines and applications, and the Modifiers page has instructions for how to turn on multiline mode in various languages. Therefore, ^a matches the a on both of these lines:Īpart from Ruby, all engines allow you to turn on a mode where the anchors ^ and $ match on every line. In Ruby, ^ always asserts that the engine's current position in the string is the beginning of a line-which is either the beginning of the string or a position following a line feed character \n. Therefore, ^a matches an a at the beginning of the string. In most engines (but not Ruby), when multiline mode hasn't been turned on, the caret anchor ^ asserts that the engine's current position in the string is the beginning of the string. Anchors on the boundaries page, and in Anchors Seen as Delimiters lower on this page. Why is ^ called an anchor while \b is called a boundary and (?