Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript replace 2 characters

Tags:

javascript

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>
like image 953
user275753 Avatar asked Feb 15 '26 05:02

user275753


1 Answers

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

like image 199
NVRAM Avatar answered Feb 16 '26 18:02

NVRAM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!