some text start:anythingcanbehere some other textIn other word, I'm trying to remove all white spaces between a specific range of such" name="description" /> some text start:anythingcanbehere some other textIn other word, I'm trying to remove all white spaces between a specific range of such" />
84492

How to execute a capturing group multiple times?

Question:

I have this string:

var str = "some text start:anything can be here some other text";

And here is expected result:

//=> some text start:anythingcanbehere some other text

In other word, I'm trying to remove all white spaces between a specific range of such a string.

<hr />

Also Here is what I have tried:

(start:)(?:(\S+)(\s+))(.*)(?= some)

<a href="https://regex101.com/r/wB9eR9/2" rel="nofollow">It works</a> as well, but I should execute it several times to achieve expected result .. How can I use \1+ in my regex to run it several times?

Answer1:

You can't do what you want with a simple regexp replace, because a capture group can only capture one string -- there's no looping. Javascript allows you to provide a function as the replacement, and it can perform more complex operations on the captured strings.

<pre class="snippet-code-js lang-js prettyprint-override">var str = "some text start:anything can be here some other text"; var newstr = str.replace(/(start:)(.*)(?= some)/, function(match, g1, g2) { return g1 + g2.replace(/ /g, ''); }); alert(newstr);

Answer2:

Using replace with a callback:

var repl = str.replace(/(start:.*?)(?= some\b)/, function(_, $1) { return $1.replace(/\s+/g, ''); }); //=> some text start:anythingcanbehere some other text

Recommend

  • Updating and removing unique join relationships in CakePHP
  • Efficient User-Agent Regex to find Safari in Python
  • How to split circle in to the sectors in google maps?
  • Validate jQuery plugin, field not required
  • Django model inheritance, filtering models
  • Reading a file into a multidimensional array
  • Security issues with PHP's Readfile method
  • Not able to aggregate on nested fields in elasticsearch
  • Unable to install Git-core+svn by MacPorts
  • Excel's Macro-Recorder usage
  • Django simple Captcha “No module named fields” error
  • onBackPressed() not being executed
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Could not find rake using whenever rails
  • Atlas images wrong size on iPad iOS 9
  • Android fill_parent issue
  • Extracting HTML between tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Regex thinks I'm nesting, but I'm not
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • How to recover from a Spring Social ExpiredAuthorizationException
  • ILMerge & Keep Assembly Name
  • How to handle AllServersUnavailable Exception
  • ActionScript 2 vs ActionScript 3 performance
  • VBA Convert delimiter text file to Excel
  • Large data - storage and query
  • Delete MySQLi record without showing the id in the URL
  • WOWZA + RTMP + HTML5 Playback?
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Matrix multiplication with MKL
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Programmatically clearing map cache
  • Reading document lines to the user (python)
  • Converting MP3 duration time
  • java string with new operator and a literal