EN
JavaScript - \R equivalent in RegEx
10
points
In this short article, we would like to show how to use \R
character class regular expressions in JavaScript what is not available by default.
Quick solution: use
\u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]
as equivalent to\R
.
Practical example:
// ONLINE-RUNNER:browser;
var LINE_EXPRESSION = /\u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]/g; // expression symbols order is very important
var text = 'line 1\n' +
'line 2\r' +
'line 3\r\n' +
'line 4\n\r' +
'line 5';
var lines = text.split(LINE_EXPRESSION);
console.log(lines);
Warning: above expression will treat
\n\r
as two new lines symbols! - it is very rare case (used on: Acorn BBC and RISC OS spooled text output)
Where:
Unicode | Description |
\u000D\000A | \r\n pair |
\u000A | Line Feed / LF (\n ) |
\u000B | Line Tabulation |
\u000C | Form Feed (\f ) |
\u000D | Carriage Return / CR (\r ) |
\u0085 | Next Line (NEL ) |
\u2028 | Line Separator |
\u2029 | Paragraph Separator |
Source
- https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html (Linebreak matcher section)