简体   繁体   中英

Replace string that starts with specific character and ends with specific character in regular expression

I wanted to replace a string section that starts with a specific character and ends with specific character. At below, I demonstrate test case.

var reg = /pattern/gi;

var str = "asdfkadsf[xxxxx]bb";

var test = str.replace(reg,"") == "asdfkadsfbb"

console.log(test);

This pattern should work for replace anything between brackets (including the brackets):

var reg = /(\[.*?\])/gi;

var str = "asdfkadsf[xxxxx]bb";

var test = str.replace(reg,"") == "asdfkadsfbb"

基于您的示例,这适用:

/\[.*]/gi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM