簡體   English   中英

如何僅從整個字符串的開頭刪除括號中的字符串?

[英]How do I remove the string in brackets only from the beginning of the entire string?

我嘗試使用replace刪除括號中的字符串。 但這不起作用。 我只想刪除開頭的括號。

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); 

請嘗試以下正則表達式,不帶/ g標志

\((.+?)\)

如果要保留方括號,您將在第1組中得到結果,

正則表達式演示

這應該在大多數(如果不是全部)實例中起作用:

myString.replace(/\\((.*?)\\)/, '$1')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM