Remove specific character from a string in JavaScript
const removeSpecificCharacter = (text, characterToRemove) => {
if (!text) {
return '';
}
const pattern = characterToRemove.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
const re = new RegExp(pattern + '$'); // From the end
// const re = new RegExp('^' + pattern); // From the start
// const re = new RegExp(pattern, 'g'); // From everywhere
return text.replace(re, "");
}
Processing text such as user inputs often requires the deletion of certain unwanted characters. You can remove a specific character from a string using the replace
method.
There may be several similar needs. Sometimes you only need to delete a particular character from the beginning of the text, and sometimes you only need to delete it from the end and there are times when you need to delete it from everywhere. This can be achieved using the appropriate RegExp constructor:
const re = new RegExp(pattern + '$'); // From the end
// const re = new RegExp('^' + pattern); // From the start
// const re = new RegExp(pattern, 'g'); // From everywhere
It is also conceivable that the character you want to delete is itself a special regular expression. In this case, the character must first be properly prefixed, such as:
const pattern = characterToRemove.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
replace(substr, newSubstr)
replace(searchValue: string | RegExp, replaceValue: string): string;
RegExp()
new (pattern: RegExp | string, flags?: string): RegExp;
const re = new RegExp(pattern + '$');
- Remove double quotes from a string in JavaScript
- Check if date is valid in JavaScript
- Check if a string is empty in JavaScript
- Check if string contains a substring in JavaScript
- Escape quotes in JavaScript
- Count the number of characters in a string with JavaScript
- Concatenate strings in JavaScript
- Check if string ends with slash using JavaScript
- Get the last character of a string in JavaScript
- Convert array to comma separated string using JavaScript
- Remove all extra spaces from a string in JavaScript
- Check if string contains a specific character using JavaScript
- Remove spaces from string at the beginning, end, or both in JavaScript
- Remove emojis from a string with JavaScript
- Check if a string ends with number in JavaScript
- Remove specific character from a string in JavaScript
- Remove protocol from URL with JavaScript
- Remove the last character from a string in JavaScript
- Count the number of words in a string using JavaScript
- Count the number of lines in a string in JavaScript
- Convert string to int in JavaScript
- Convert string to lowercase with JavaScript
- Generate random string in JavaScript
- Generate UUID in JavaScript
- Generate random color with JavaScript
- Convert string to kebab case in JavaScript
- Convert snake case to camel case in JavaScript
- Capitalize words in a string using JavaScript
- Convert string to character array in JavaScript
- Convert camel case to snake case in JavaScript
- Remove accents from a string in JavaScript
- Convert a string to sentence case in JavaScript
- Convert string to title case in JavaScript
- Convert string to snake case in JavaScript
- Convert string to pascal case in JavaScript
- Convert string to camel case in JavaScript
- Convert string to uppercase with JavaScript
- Convert object to JSON string in JavaScript
- Parse JSON string in JavaScript
- convert number to string in JavaScript
- Use colors in JavaScript console
- Get string length in JavaScript
- Split a string into words
- Replace line break with br in JavaScript
- Replace spaces with dashes in JavaScript
- Create multiline string in JavaScript