Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find a form feed character with JavaScript RegExp.
Form feed character is a page breaking ASCII control character commonly used for page separators. When you want to insert a page break, text editors can use this form feed. It is defined as \f and has an ASCII code value of 12 or 0x0c.
RegExp is an object that specifies the pattern used to perform search and replace operations on strings or for input validation. RegExp was introduced in ES1 and is fully supported by all browsers.
Now, we will check how to find the form feed (\f) character in given text using RegExp.
Syntax
Following is the syntax for form feed character pattern:
new RegExp("\f") or simply /\f/
/\f/ was introduced in ES1 and is fully supported by all browsers including Chrome, IE, Safari, Opera, Firefox, and Edge.
Finding Form Feed Character Position
The search() method returns the index of the first match, or -1 if not found.
<html>
<body>
<p>Finding the position of a form feed character</p>
<p>The position is: <span id="result"></span></p>
<script>
let text = "Hello, Devika. \fWelcome back";
let pattern = /\f/;
let result = text.search(pattern);
document.getElementById("result").innerHTML = result;
</script>
</body>
</html>
The position is: 15
Handling Non-Existent Form Feed Character
When no form feed character is found, the search method returns -1:
<!DOCTYPE html>
<html>
<body>
<h2>RegExp \f finding</h2>
<p id="result"></p>
<script>
const text = "Hello, Devika. Welcome back";
const regExp = /\f/;
const output = text.search(regExp);
if(output == -1) {
document.getElementById("result").innerHTML = "No form feed character present.";
} else {
document.getElementById("result").innerHTML = "Index of form feed character: " + output;
}
</script>
</body>
</html>
No form feed character present.
Replacing Form Feed Characters with split() and join()
You can replace form feed characters using the split() and join() methods:
<!DOCTYPE html>
<html>
<body>
<h1>Replace form feed character</h1>
<p>After replacing the form feed character: <span id="result"></span></p>
<script>
var text = "Hello,\fDevika.\fWelcome back.";
var result = text.split("\f").join(" ");
document.getElementById("result").innerHTML = result;
</script>
</body>
</html>
After replacing the form feed character: Hello, Devika. Welcome back.
Replacing Form Feed Characters with replace()
The replace() method with global flag replaces all occurrences:
<!DOCTYPE html>
<html>
<body>
<h1>Replace form feed character</h1>
<p>After replacing the form feed character: <span id="result"></span></p>
<script>
var text = "Hello,\fDevika.\fWelcome back.";
var result = text.replace(/\f/g, " ");
document.getElementById("result").innerHTML = result;
</script>
</body>
</html>
After replacing the form feed character: Hello, Devika. Welcome back.
RegExp Modifiers
RegExp supports several modifiers:
- g - Global matching (finds all occurrences)
- i - Case-insensitive matching
- m - Multiline matching
Conclusion
Use the /\f/ pattern to find form feed characters in JavaScript strings. The search() method locates the position, while replace() with the global flag can remove or replace all occurrences effectively.
