JavaScript Remove Hyphens from a String

This article will remove all dashes (‘-‘) from a javascript string using different methods and example illustrations. While developing in any language, removing dashes or hyphens from a string is common before processing it, as it might be a phone number, for example.

Table of Contents:-

Javascript String Remove Parenthesis using replace()

Javascript’s replace() method finds a pattern to replace some or all of its occurrences with a replacement(character/string) that can be a character or a string or a regular expression.

Regular Expressions are patterns used to match character combinations in a string.

syntax:

Advertisements
replace(regExp, newSubstring)
  • regExp: is the regular expression object to be used.
  • newSubstring: is the string that will replace the pattern.

Example:-

Remove all dashes from the string “0042-9876-55649”

Code:-

let dummyString = "0042-9876-55649";
let finalString = dummyString.replace(/-/g, '');
console.log("Original String: " + dummyString);
console.log("Final String: " + finalString);

Output:-

Original String: 0042-9876-55649
Final String: 0042987655649

Explanation

Here in the above code, we are using replace() method with RegExp. First argument: regular expression /-/g, and second is the replacement string, nothing (”) in our case.

  • Regular Expression Reference:
  • / and / specify the beginning and end of the string.
  • specifies to match the hyphen.
  • g specifies the global search to match all the occurrences within the string.

Javascript String Remove Parenthesis using replaceAll()

Javascript’s replaceAll() method replaces all the pattern matches within a string with a replacement. The first argument is a pattern that can be a string or a regular expression that needs to be searched and replaced with a replacement. The second argument is a replacement string or a function.

Example:-

Remove all dashes from the string “0042-9876-55649”

Code:-

let dummyString = "0042-9876-55649";
let stringWithoutHyphen = dummyString.replaceAll('-', '');
console.log("Original String: " + dummyString);
console.log("Final String: " + stringWithoutHyphen);

Output:-

Original String: 0042-9876-55649
Final String: 0042987655649

Explanation

Here, replaceAll() method is used to replace the character (‘-‘) with nothing (”).

Javascript String Remove Hyphens using split() and join()

The split() method in javascript returns an array of substrings formed by splitting a given string.

The join() method in javascript joins the array elements back into a string.

Example:-

Remove all dashes from the string “0042-9876-55649”

Code:-

let dummyString = "0042-9876-55649";
let stringWithoutHyphen = dummyString.split('-').join('');
console.log("Original String: " + dummyString);
console.log("Final String: " + stringWithoutHyphen);

Output:-

Original String: 0042-9876-55649
Final String: 0042987655649

Explanation:-

The above code shows that we split the string based on a character that is hyphen/ dash (‘-‘) using the split() method into an array of substrings. This array will have elements {0042, 9876, 55649}. Then join the array back into one string using the join() method.

I hope this article helped you delete all dashes from a string in javascript. Good Luck !!!

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top