Separate bracket in js regx | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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]’

2nd Oct 2018, 7:06 PM
DONG PEI
DONG PEI - avatar
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....😅
2nd Oct 2018, 7:20 PM
Burey
Burey - avatar
+ 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, " [")
2nd Oct 2018, 7:24 PM
Burey
Burey - avatar
+ 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
2nd Oct 2018, 11:57 PM
Calviղ
Calviղ - avatar