Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a semicolon in a string by JavaScript [duplicate]

Tags:

javascript

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?

like image 944
Jin Yong Avatar asked Jan 10 '10 23:01

Jin Yong


People also ask

How do you remove a semicolon from a string?

str = str. replace(/;/g, ""); This will remove all semicolons in str and assign the result back to str .

Can you omit semicolon in JavaScript?

To recap, semicolons are not mandatory in JavaScript. Instead, the Automatic Semicolon Insertion (ASI) process adds semicolons where necessary.

Is semi colon used in JavaScript?

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.


1 Answers

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.

like image 164
Darko Z Avatar answered Sep 26 '22 02:09

Darko Z