Find a form feed character with JavaScript RegExp.


Form feed character is page breaking ASCII control character. It is commonly used for page separators. When you want to insert a page break, the text editor can use this form feed. It is defined as \f and has an ASCII code value as 12 or 0x0c.

RegExp is an object that specifies the pattern used to do a search and replace operations on a string or for input validation. RegExp was introduced in ES1, and it is fully supported by all browsers.

Now, we will check how to find the form feed (\f) character in the given text using RegExp.

Syntax

Following is the syntax for form feed character −

new RegExp("\f") or simply /\f/

/\f/ is introduced in ES1. It is fully supported by all browsers. Like, such as Chrome, IE, Safari, Opera, Firefox, and Edge.

Algorithm

  • STEP 1 − Define a string having at least a form feed character.
  • STEP 2 − Define the RegExp pattern for form feed character.
  • STEP 3 − Apply search(pattern) on the above-defined string to find the index of the form feed character.
  • STEP 4 − Display the result, the index of the form feed character.

Notice that using the above approach, we will find the index of the form feed character in the given string.

Example 1

In the program below, we use string search(pattern) to find a form feed character (\f) in the input string. We use RegExp as /\f/. The string search() method returns the index of the matched character. So it returns the index of the form feed character.

<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>

If the form feed character is found it will return the position else it will return -1.

Example 2

In the program below, we take a string with no form feed character and try to find a form feed character in the string. See how our output looks like.

<!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>

Example 3

Finding and Replacing the form feed character

We can also replace the form feed characters in a given text. In the program below, we find the form feed character and replace it with the space using the String split() method.

<!DOCTYPE html> <html> <body> <h1>Replace form feed line 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>

Example 4

In the program below, we find the form feed character and replace it with the space using the String replace() method.

<!DOCTYPE html> <html> <body> <h1>Replace form feed line 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>

Here, "g" for performing global matches i.e. instead of stopping at the first occurance, it will look for all the occurrences. We can replace the form feed characters in other ways also. Here, I have just explained a few simpler ways.

RegExp has modifiers like g, i, m. "g" for performing global matches, "i" for performing case-insensitive matching and "m" for performing multiline matching.

We have seen how to find the form feed character in given text using RegExp in JavaScript. Hope this gives clarification.

Updated on: 26-Aug-2022

849 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements