92

I have IDs that look like: 185-51-671 but they can also have letters at the end, 175-1-7b

All I want to do is remove the hyphens, as a pre-processing step. Show me some cool ways to do this in javascript? I figure there are probably quite a few questions like this one, but I'm interested to see what optimizations people will come up with for "just hyphens"

Thanks!

edit: I am using jQuery, so I guess .replace(a,b) does the trick (replacing a with b)

numberNoHyphens = number.replace("-","");

any other alternatives?

edit #2:

So, just in case anyone is wondering, the correct answer was

numberNoHyphens = number.replace(/-/g,"");

and you need the "g" which is the pattern switch or "global flag" because

numberNoHyphens = number.replace(/-/,"");

will only match and replace the first hyphen

1
  • 1
    You don't need to annotate the title of your question with [solved] - this is implied by giving the 'green check' to the answer that solved your problem. Questions with accepted answers are visually distinct on StackOverflow from questions without an accepted answer.
    – Matt
    Jun 1, 2011 at 17:59

6 Answers 6

191

You need to include the global flag:

var str="185-51-671";
var newStr = str.replace(/-/g, "");
5
  • 1
    Is there a way of just trimming the hyphens at the start of the string and leaving the ones in the middle of the string? so '--this-is-a-string' would become 'this-is-a-string'
    – abyrne85
    Oct 4, 2018 at 10:46
  • @abyrne85, yes, there is a way, however, it would be best if you asked a new question.
    – James Hill
    Oct 4, 2018 at 11:03
  • this gives me an error in my IDE: expected expression, found `/`
    – Jim
    Feb 19, 2022 at 23:52
  • To remove the hyphens from the beginning of the string: var str = '--- te - st end -'; str = str.replace(/^-+/g, '');. Then .trim() to remove whitespaces too. Or use this regex: str = str.replace(/^(-|\s+)+/g, '');
    – Avatar
    Jul 9, 2022 at 5:54
  • @Avatar neither of the examples that you provided work as the OP requested. I suggest instead of creating a new answer in the comments that you should actually create a new answer...then we can downvote it :-)
    – James Hill
    Jul 21, 2022 at 10:01
14

This is not faster, but

str.split('-').join('');

should also work.

I set up a jsperf test if anyone wants to add and compare their methods, but it's unlikely anything will be faster than the replace method.

http://jsperf.com/remove-hyphens-from-string

0
8
var str='185-51-671';
str=str.replace(/-/g,'');
1
  • ...and it seems like both of you (you and CrisDeBlonde) answered this one at the exact same instant haha
    – sova
    Jun 1, 2011 at 16:59
8

Gets much easier in String.prototype.replaceAll(). Check out the browser support for the built-in method.

const str = '185-51-671';
console.log(str.replaceAll('-', ''));

1
  • 2
    What's up my 2021 people! This has lots of modern support and has high readability. str.replaceAll should be the accepted answer as of now, as long as you don't need to cover one off or unsupported browsers. (RIP Internet Explorer.) In case you are learning it, if you need a space to replace what you are removing, simply add a space to the 2nd argument: str.replaceAll("removeditem", " ");
    – DORRITO
    Jul 6, 2021 at 22:56
0

Som of these answers, prior to edits, did not remove all of the hyphens. You would need to use .replaceAll("-","")

2
-4

In tidyverse, there are multiple functions that could suit your needs. Specifically, I would use str_remove, which will replace in a string, the giver character by an empty string (""), effectively removing it (check here the documentation). Example of its usage:

str_remove(x, '-') 
4
  • 1
    A good answer will always include an explanation why this would solve the issue, so that the OP and any future readers can learn from it.
    – Tyler2P
    Dec 23, 2021 at 9:55
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. Dec 23, 2021 at 10:16
  • 1
    Why do you answer a 10-year-old JavaScript question that has nothing to do with the R programming language with a solution in tidyverse? Dec 24, 2021 at 22:38
  • In addition to this being in the wrong language, R doesn't differentiate between single and double quotes. Either one would work in R
    – camille
    Dec 26, 2021 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.