我尝试使用替换来删除括号中的字符串。但这是行不通的。我只想删除开头的括号。
var string="(I want to delete this) helloo (not this)";
var replacedString=string.replace(/ *\([^)]*\) */g, "");
最佳答案
您可以在字符串的开头查找第一个括号和结束括号,然后替换它。
var string = "(I want to delete this) helloo (not this)",
result = string.replace(/^\([^)]*\)/, '');
console.log(result);
关于javascript - 如何仅从整个字符串的开头删除括号中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44000818/