quake3 server connector | dns lookup | quake3 server status | Search

The removeCtrlChars function removes control characters and trims whitespace from a given string, returning the processed string. It uses regular expressions to match and remove control characters in specific formats, and is exported as a module for use in other parts of the application.

Run example

npm run import -- "remove ctrl characters"

remove ctrl characters


// TODO: use this for server names and player names when matching to discord
function removeCtrlChars(str) {
    return str
        .replace(/\^\^[a-z0-9][a-z0-9]/ig, '')
        .replace(/\^[a-z0-9]/ig, '')
        .trim()
}

module.exports = removeCtrlChars

What the code could have been:

/**
 * Removes control characters from a string, specifically ^ and its lowercase letter variations.
 * 
 * @param {string} str - The input string to process.
 * @returns {string} The input string with control characters removed.
 */
function removeControlCharacters(str) {
    // Use a regular expression with the 'g' and 'i' flags to replace all occurrences of the control characters.
    return str.toLowerCase()
       .replace(/[\^~`|{}[\]()<>+?*.-]/g, '') // Add more control characters
       .trim();
}

module.exports = removeControlCharacters;

Function: removeCtrlChars

Description

Removes control characters and trim whitespace from a string.

Parameters

Regular Expressions Used

Output

The function returns the processed string with control characters and whitespace removed.

Export

The function is exported as a module, allowing it to be used in other parts of the application.

Example Usage

const removeCtrlChars = require('./removeCtrlChars');

console.log(removeCtrlChars('^Hello^World'));  // Output: "HelloWorld"