tags:

views:

40

answers:

5

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

it is because i can only replace either ( or ) with "" instead of both

how to achieve the goal??

that is

original: (abc, def)

modified: abc, def

thanks

my code:

<html>
<body>

<script type="text/javascript">

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

</script>
</body>
</html>
+1  A: 

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

Vinay B R
+1  A: 

You could use a regex, or

<html>
<body>

<script type="text/javascript">

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

</script>
</body>
</html>
Nik
+2  A: 

Use a regexp, and the g for global replacements:

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

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

Carter Galle
A: 

An alternative version using a single regexp is str.replace(/\(|\)/g,"");

Minkiele
A: 

If the parentheses will be the first and last characters, you could avoid a regular expression by using .substring().

Example: http://jsfiddle.net/nRh3C/

var string =  "(abc, def)";

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

or using .substr():

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

var string =  "(abc, def)";

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