18

How can I remove the semicolon (;) from a string by using JavaScript?

For example:

var str = '<div id="confirmMsg" style="margin-top: -5px;">'

How can I remove the semicolon from str?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jin Yong
  • 42,698
  • 72
  • 141
  • 187
  • I rolled this back to contain the actual, original example; check the source in the history to see that this was the original example, Gumbo had just fixed the formatting. – Brian Campbell Jan 11 '10 at 00:39

5 Answers5

22

You can use the replace method of the string object. Here is what W3Schools says about it: JavaScript replace().

In your case you could do something like the following:

str = str.replace(";", "");

You can also use a regular expression:

str = str.replace(/;/g, "");

This will replace all semicolons globally. If you wish to replace just the first instance you would remove the g from the first parameter.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Darko
  • 38,310
  • 15
  • 80
  • 107
8

Try this:

str = str.replace(/;/g, "");

This will remove all semicolons in str and assign the result back to str.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
6

Depending upon exactly why you need to do this, you need to be cautious of edge cases:

For instance, what if your string is this (contains two semicolons):

'<div id="confirmMsg" style="margin-top: -5px; margin-bottom: 5px;">'

Any solution like

 str.replace(";", "");

will give you:

'<div id="confirmMsg" style="margin-top: -5px margin-bottom: 5px">'

which is invalid.

In this situation you're better off doing this:

 str.replace(";\"", "\"");

which will only replace ;" at the end of the style string.

In addition I wouldn't worry about removing it anyway. It shouldn't matter - unless you have already determined that for your situation it does matter for some obscure reason. It's more likely to lead to hard-to-debug problems later if you try to get too clever in a situation like this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • 1
    +1 for mentioning that there is no obvious reson to remove the semicolon (and for the edge cases). – Guffa Jan 10 '10 at 23:54
  • just need to mention that the case given in question was not given by the asker but by Gumbo in an edit. Check out the edit history. – Darko Jan 11 '10 at 00:30
  • No, it wasn't added by Gumbo in his edit. The example was there, but as it was raw, unquoted HTML, it was stripped out when Stack Overflow rendered it; view the source on the original post to confirm. Gumbo just fixed the formatting so the example would display. – Brian Campbell Jan 11 '10 at 00:36
2
str = str.replace(";", "");
Li0liQ
  • 11,158
  • 35
  • 52
0

Try:

str = str.replace(/;/ig,'');
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yasir Laghari
  • 10,714
  • 4
  • 19
  • 17
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Jul 24 '16 at 14:51