Javascript replace 2 characters

I would like to ask how to replace char "(" and ")" with ""

this is because I can replace either (or) with "" instead of both

how to achieve the goal?

i.e

original: (abc, def)

changed: abc, def

thank

my code is:

<html>
<body>

<script type="text/javascript">

var str="(abc, def)";
document.write(str.replace("(",""));

</script>
</body>
</html>
+3
source share
5 answers

Use regex and g for global notes:

var str="(abc, def)";
document.write(str.replace(/[()]/g,''));

For reference: http://javascriptkit.com/jsref/regexp.shtml

+6
source

You can use regex or

<html>
<body>

<script type="text/javascript">

var str="(abc, def)";
document.write(str.replace("(","").replace(")",""));

</script>
</body>
</html>
+3
source

, .substring().

: http://jsfiddle.net/nRh3C/

var string =  "(abc, def)";

alert( string.substring(1, string.length-1) );

.substr():

: http://jsfiddle.net/nRh3C/1/

var string =  "(abc, def)";

alert( string.substr(1, string.length -2) );
+2

use str.replace(/\(/g,'').replace(/\)/g,'');

+1
source

Alternate version using one regex str.replace(/\(|\)/g,"");

0
source

Source: https://habr.com/ru/post/1766627/


All Articles