Open In App

JavaScript RegExp \r Metacharacter

Last Updated : 06 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The \r metacharacter in JavaScript regular expressions matches a carriage return character. A carriage return is a special control character (\r) with the ASCII code 13, often used to represent the end of a line in text files created on older operating systems (e.g., Windows uses \r\n for line breaks).

JavaScript
let regex = /\r/g;
let str = "Hello\rWorld";
let matches = str.match(regex);
console.log(matches); 

Output
[ '\r' ]

The pattern \r matches the carriage return character in the string.

Syntax:

/\r/

Matches a single carriage return character (\r).

Key Points

  • Carriage Return Only: Matches the \r character, not other line-ending characters like \n.
  • Line Breaks in Windows: Often appears alongside \n as part of a Windows-style newline (\r\n).
  • Use Cases: Detect or handle text with Windows-style line endings or sanitize input text.

Real-World Examples

1. Detecting Carriage Return

JavaScript
let regex = /\r/;
let str = "Line1\rLine2";
if (regex.test(str)) {
    console.log("Carriage return found.");
} else {
    console.log("No carriage return found.");
}

Output
Carriage return found.

Here, the \r metacharacter detects the presence of a carriage return in the string.

2. Replacing Carriage Returns

JavaScript
let regex = /\r/g;
let str = "Hello\rWorld";
let result = str.replace(regex, "");
console.log(result); 

Output
HelloWorld

Using \r with replace(), you can remove carriage return characters from a string.

3. Handling Windows-Style Newlines

JavaScript
let regex = /\r\n/g;
let str = "Line1\r\nLine2";
let result = str.replace(regex, "\n");
console.log(result); 

Output
Line1
Line2

The pattern \r\n matches Windows-style line endings and converts them to a Unix-style newline (\n).

4. Splitting Text with Carriage Returns

JavaScript
let regex = /\r/;
let str = "Line1\rLine2\rLine3";
let parts = str.split(regex);
console.log(parts); 

Output
[ 'Line1', 'Line2', 'Line3' ]

Here, \r is used to split the string into parts wherever a carriage return is found.

5. Normalizing Line Endings

JavaScript
let regex = /\r/g;
let str = "Hello\r\nWorld\r";
let normalized = str.replace(regex, "");
console.log(normalized); 

Output
Hello
World

This example removes all carriage returns, leaving only Unix-style line endings.

Common Patterns Using \r

  • Match Windows Line Breaks:
/\r\n/
  • Remove Carriage Returns:
str.replace(/\r/g, "")
  • Detect Line Breaks:
/(\r\n|\r|\n)/g
  • Split on Carriage Returns:
str.split(/\r/)

Why Use \r?

  • Cross-Platform Text Handling: Handle text files with platform-specific line endings (e.g., Windows vs. Unix).
  • Sanitizing Input: Remove unnecessary control characters from input strings.
  • Text Normalization: Ensure consistency in line endings across different systems.

Conclusion

The \r metacharacter is essential for working with text files or handling input strings containing Windows-style line endings.

Recommended Links:



Next Article

Similar Reads