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
?
str = str. replace(/;/g, ""); This will remove all semicolons in str and assign the result back to str .
To recap, semicolons are not mandatory in JavaScript. Instead, the Automatic Semicolon Insertion (ASI) process adds semicolons where necessary.
Semicolons in JavaScript divide the community. Some developers prefer to use them always. Few developers want to avoid them. In some cases, omitting them may lead to bad consequences.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With