JavaScript Javascript removes letters duplicates and sequence in string

There are many posts like this and I have found a few solutions but there are not perfect. One of them:

"aabbhahahahahahahahahahahasetsetset".replace(/[^\w\s]|(.+)\1+/gi, '$1')

The results is:

abhahahahahahaset

I want to get result:

abhaset

How to do this ?

Answer:1

.+ is greedy. It takes as much as it can. That is half of the has so that \1 can match the second half. Making the repetition ungreedy should do the trick:

/[^\w\s]|(.+?)\1+/gi

By the way, the i doesn't change anything here.

To get rid of nested repetitions (e.g. transform aaBBaaBB into aB (via aaBB or aBaB)) simply run the replacement multiple times until the result does not change any more.

var pattern = /[^\w\s]|(.+?)\1+/g;

var output = "aaBBaaBB";
var input;

do
{
    input = output;
    output = input.replace(pattern, "$1");
} while (input != output)

I admit the naming of output is a bit awkward for the first repetition, but you know... the two most difficult problems in computer science are cache invalidation, naming things and off-by-one errors.

Answer:2

I need to load 3 jquery scripts in a page. For business reasons i've to make the minimum modifications possible so i've decided to create one javascript to be included in the website. After that, ...

I need to load 3 jquery scripts in a page. For business reasons i've to make the minimum modifications possible so i've decided to create one javascript to be included in the website. After that, ...

  1. load jquery javascript
  2. load jquery javascript function
  3. load jquery javascript library
  4. load jquery js
  5. load jquery js file
  6. jquery load javascript file
  7. jquery load javascript not working
  8. jquery load javascript equivalent
  9. jquery load javascript dynamically
  10. jquery load javascript file dynamically
  11. jquery load javascript on demand
  12. jquery load javascript and execute
  13. jquery load javascript in html
  14. jquery load javascript variable
  15. jquery load javascript code
  16. jquery load javascript file asynchronously
  17. jquery load javascript ajax
  18. jquery load javascript source
  19. jquery load js script
  20. jquery load js dynamically

I am developing a mobile hybrid application on BlueMix. I have just added the necessary tools by following the instructions here. Server Node.js code (dependencies and ibmconfig variable are ...

I am developing a mobile hybrid application on BlueMix. I have just added the necessary tools by following the instructions here. Server Node.js code (dependencies and ibmconfig variable are ...

Is there a way to create a custom animation event with angularJS. I am trying to create my own event of trigger an event on my own. I'd like to do the following <div ng-animate="{myEvent: 'my-...

Is there a way to create a custom animation event with angularJS. I am trying to create my own event of trigger an event on my own. I'd like to do the following <div ng-animate="{myEvent: 'my-...

I have this code in JQUERY: var large='<div class="vip" style ="background:url(./uploads/deal/"+data[a].id+"_1.jpg);"> <span class ="coundtown" value ='+data[a].deal_time_off+' ...

I have this code in JQUERY: var large='<div class="vip" style ="background:url(./uploads/deal/"+data[a].id+"_1.jpg);"> <span class ="coundtown" value ='+data[a].deal_time_off+' ...

  1. uncaught syntaxerror unexpected token ' '
  2. uncaught syntaxerror unexpected identifier
  3. uncaught syntaxerror unexpected end of input
  4. uncaught syntaxerror unexpected token 'export'
  5. uncaught syntaxerror unexpected end of json input
  6. uncaught syntaxerror unexpected token ' ' react
  7. uncaught syntaxerror unexpected token in json at position 0
  8. uncaught syntaxerror unexpected string
  9. uncaught syntaxerror unexpected token ' ' angular
  10. uncaught syntaxerror unexpected identifier javascript
  11. uncaught syntaxerror unexpected token 'else'
  12. uncaught syntaxerror unexpected number
  13. uncaught syntaxerror unexpected token 'if'
  14. uncaught syntaxerror unexpected token javascript
  15. uncaught syntaxerror unexpected token 'var'
  16. uncaught syntaxerror unexpected token 'function'
  17. uncaught syntaxerror unexpected token 'export' popper.js
  18. uncaught syntaxerror unexpected reserved word
  19. uncaught syntaxerror unexpected token '.' css
  20. uncaught syntaxerror unexpected identifier jquery