用新的字符串替换字符串的中间部分

时间:2013-10-20 18:08:30

标签: javascript

我有一个字符串如下:

var email = "name(replace these parenthesis by @)domain.com"

如何使用javascript替换部件(replace these parenthesis by @)

4 个答案:

答案 0 :(得分:4)

email.replace("(replace these parenthesis by @)", "@")

答案 1 :(得分:2)

如果您确定模式,可以使用正则表达式。 @)之间的每个字符都将替换为 @

var str = "name(replace these parenthesis by @)domain.com";
var patt1 = /\(.*@\)/i;
document.write(str.replace(patt1,"@"));

答案 2 :(得分:1)

表示您要呈现的字符串格式:

var email = "name(replace these parenthesis by @)domain.com";

这样做:

email.replace(/\(.*\)/g,"@");

将给出

name@domain.com

答案 3 :(得分:0)

如果内容与您提到的(replace these paranthesis by @)相同,则可以将其替换为:

var email = "name(replace these parenthesis by @)domain.com";
email.replace(/(replace these parenthesis by @)/g,"@");

或者您正在寻找更通用的东西吗?