0
Separate bracket in js regx
How do we write expressions to add a space between bracket in js For example, âAvc[1234]â How to make it âAvc [1234]â
3 Answers
+ 9
Most straightforward way would probably be
"Avc[123]".replace("[", " ["))
i'm not a regex expert but there are probably more "correct" ways....đ
+ 8
If you want it to replace all occuurences (in case there are more than one) then do this using global flag
"Avc[123] and Avc[455]".replace(/\[/g, " [")
+ 3
const regex = /(\w)([ ]*)(\[\d+])/gm;
const str = `avc[123]
avc [123]
avc [123]
avc [123]
avcd[12345]
[123]
avc[123
avc123
avc`;
const subst = `$1 $3`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
More info
https://regex101.com/r/k8XD9D/2