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.
npm run import -- "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
/**
* 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;
Removes control characters and trim whitespace from a string.
str
: The input string to process.^\^[a-z0-9][a-z0-9]
: Matches and removes control characters in the format `^ followed by a lowercase letter or number followed by zero or more lowercase letters or numbers.^\^[a-z0-9]
: Matches and removes control characters in the format `^ followed by a lowercase letter or number.\s+
: Not used in the original function, but commonly used to trim whitespace.The function returns the processed string with control characters and whitespace removed.
The function is exported as a module, allowing it to be used in other parts of the application.
const removeCtrlChars = require('./removeCtrlChars');
console.log(removeCtrlChars('^Hello^World')); // Output: "HelloWorld"