1

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, '-')
1
  • 1
    javascript, but if you have a solution in Python I can understand that as well. Or even regex101. I want to understand how the regex works. Commented Nov 17, 2021 at 16:10

2 Answers 2

1

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'

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
Sign up to request clarification or add additional context in comments.

2 Comments

Could you maybe add a quick explanation as to what is going on?
I have added some explanation in my answer. Feel free to ask any query/doubt about the solution.
1

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)) )

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, '-') )

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.