简体   繁体   中英

Always adding a whitespace between two characters with regex

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:

  1. To always ensure whitespace between two curly braces
  2. 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 { { { .

It doesn't work as you expect it because the first two { characters match the regex , the replacement is made then the search continues with the 3 rd character of the input string

You can solve this by turning the second parenthesis into a forward assertion . This way, the second } is not consumed on the first match and the next search starts with the second character of the input string:

preg_replace('/({|}|{|}|{|})(?=\S)/', '$1 ', $value);

There is only one capture group this way, $2 is always empty and it is not needed any more in the replacement string.

See it in action: https://3v4l.org/CaMHS . The +++ markers were added to show that the third { doesn't match the regex and no replacement occurs for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM