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);
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);
Share
Improve this question
edited Nov 7, 2017 at 14:44
Andrey Belykh
2,6544 gold badges34 silver badges49 bronze badges
asked Apr 1, 2013 at 16:56
FreshbloodFreshblood
6,43110 gold badges61 silver badges98 bronze badges
5
- In your example, you want to replace everything between the brackets? – Adam Plocher Commented Apr 1, 2013 at 16:57
-
Did you intend to replace
[xxxxx]
byb
or was that a typo and the result should beasdfkadsfbb
instead? – Jerry Commented Apr 1, 2013 at 16:58 - @AdamPlocher Yes but including brackets too. – Freshblood Commented Apr 1, 2013 at 16:59
- @jerry I removed b character now. – Freshblood Commented Apr 1, 2013 at 17:00
- +1 for providing the test-case code. – MikeM Commented Apr 1, 2013 at 18:05
2 Answers
Reset to default 11This 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"
based on your example, this works:
/\[.*]/gi