Home > database >  use regex to replace spaces that occur with a value depending on how many spaces found
use regex to replace spaces that occur with a value depending on how many spaces found

Time:11-18

I want to use a regex that looks for spaces with a minimum length of 2 in a row, and replaces the occurrence with another value for each occurrence of the space found.

For example:

I love   to eat    cake

There are 3 spaces after love and 4 spaces after eat. I want my regex to replace occurrences of a space more than 1, and to replace it with a value for each occurrence found.

The output I am trying to go for:

I love---to eat----cake

I tried something like

myStr.replace(/  {2,}/g, '-')

CodePudding user response:

You may use this code with a lookahead and a lookbehind:

const s = 'I love   to eat    cake'

var r = s.replace(/ (?= )|(?<= ) /g, '-');

console.log(r);
//=> 'I love---to eat----cake'
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

RegEx Details:

  • (?= ): Match a space only if that is followed by a space
  • |: OR
  • (?<= ) : Match a space only if that is preceded by a space

CodePudding user response:

You can match two or more whitespaces and replace with the same amount of hyphens:

const s = 'I love   to eat    cake'
console.log(s.replace(/\s{2,}/g, (x) => '-'.repeat(x.length)) )
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

The same approach can be used in Python (since you asked), re.sub(r'\s{2,}', lambda x: '-' * len(x.group()), s), see the Python demo.

Also, you may replace any whitespace that is followed with a whitespace char or is preceded with whitespace using

const s = 'I love   to eat    cake'
console.log(s.replace(/\s(?=\s|(?<=\s.))/gs, '-') )
console.log(s.replace(/\s(?=\s|(?<=\s\s))/g, '-') )
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

See this regex demo. Here, s flag makes . match any char. g makes the regex replace all occurrences. Also,

  • \s - matches any whitespace
  • (?=\s|(?<=\s.)) - a positive lookahead that matches a location that is immediately followed with a whitespace char (\s), or (|) if it is immediately preceded with a whitespace and any one char (which is the matched whitespace). If you use (?<=\s\s) version, there is no need of s flag, \s\s just makes sure the whitespace before the matched whitespace is checked.
  • Related