gpt4 book ai didi

javascript - 如何仅从整个字符串的开头删除括号中的字符串?

转载 作者:行者123 更新时间:2023-12-03 04:19:45 26 4
gpt4 key购买 nike

我尝试使用替换来删除括号中的字符串。但这是行不通的。我只想删除开头的括号。

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com