For those regex experts out there, can you help me tweak this regex to always add a whitespace between two matching characters.
My attempt is to resolve the interpolation security issue with Vue.js for data passed from the server side (Vue.js will try to evaluate anything between two curly braces). The goal is:
- To always ensure whitespace between two curly braces
- Not add additional whitespace where unnecessary.
My str_replace solution (which accomplishes goal #1 only)
str_replace(
['{', '}', '{', '}', '{', '}' ],
['{ ', '} ', '{ ', '} ', '{ ', '} '],
$value
);
My attempted regex thus far:
preg_replace('/({|}|{|}|{|})(\S)/', '$1 $2', $value);
So it checks for any matching character that isn't followed by whitespace and inserts whitespace between the two characters.
The regex works in most cases, but fails when you have three or more matching characters.
Ex: {{{
returns { {{
but the expected output would be { { {
.