Separate bracket in js regx | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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 Respostas
+ 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