Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript替换2个字符_Javascript - Fatal编程技术网

javascript替换2个字符

javascript替换2个字符,javascript,Javascript,我想问一下,如何将char(“and”)替换为“” 这是因为我只能用“”替换其中一个(或),而不能同时替换两者 如何实现目标 就是 原件:(abc,def) 修改:abc,def 谢谢 我的代码: <html> <body> <script type="text/javascript"> var str="(abc, def)"; document.write(str.replace("(","")); </script> </body

我想问一下,如何将char(“and”)替换为“”

这是因为我只能用“”替换其中一个(或),而不能同时替换两者

如何实现目标

就是

原件:(abc,def)

修改:abc,def

谢谢

我的代码:

<html>
<body>

<script type="text/javascript">

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

</script>
</body>
</html>

var str=“(abc,def)”;
文件写入(str.replace(“(”,”);

使用
str.replace(/\(/g')).replace(/\)/g');
您可以使用正则表达式,或者

<html>
<body>

<script type="text/javascript">

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

</script>
</body>
</html>

var str=“(abc,def)”;
写(str.replace(“(”,”).replace(“),”);

使用regexp,全局替换使用g

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

参考:

使用单个regexp的替代版本是
str.replace(/\(|\)/g,”;
如果括号是第一个和最后一个字符,可以通过

示例:

或:

示例:


这个方法实际上是安全和干净的,因为replace()的链接工作得很好,即使给定要替换的元素实际上不存在于字符串中。(即使字符串中不存在“(”或“))
var string =  "(abc, def)";

alert( string.substring(1, string.length-1) );
var string =  "(abc, def)";

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