2

I've got a string like this:

var main_str = 'Hi I have |str| way |str||str| too |str||str||str| many breaks';

I need to remove any |str| that occur next to another |str|.

Wanted result:

var main_str = 'Hi I have |str| way |str| too |str| many breaks |str|';

In other words if there are many |str| next to each other, we remove all but one.

tcooc
  • 20,629
  • 3
  • 39
  • 57
Mohsen Shakiba
  • 1,762
  • 3
  • 22
  • 41

2 Answers2

3
var main_str = 'Hi I have |str| way |str||str| too |str||str||str| many breaks';
main_str = main_str.replace(/(\|str\|){2,}/g, '|str|');

console.log(main_str);
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
1

Another easy way to do this is to .split() the string based on whatever delimiter you plan on using, .filter() for non-empty elements, and then .join() with the same delimiter:

JavaScript

var str = "Hi I have |str| way |str||str| too |str||str||str| many breaks";

var x = str.split("|str|").filter(function (d){
    return d.length;
}).join("|str|");

console.log(x)
// returns "Hi I have |str| way |str| too |str| many breaks |str|";

This allows you to avoid making a specific regular expression for each case—no escaping characters—and instead run variables through .split() and .join().

Not sure whether or not this solution is faster. It seems to be faster in non-Chromium browsers. I would surmise that Chrome is efficiently, because of the v8 engine, compiling a regex before running it a million times, making the regex solution faster.

fiddle

Community
  • 1
  • 1
royhowie
  • 11,075
  • 14
  • 50
  • 67
  • your answer is pretty good too :) but I guess the other one is easier – Mohsen Shakiba Jul 21 '14 at 19:40
  • @user3425760 While slightly more verbose, this solution is faster. – royhowie Jul 21 '14 at 19:41
  • How much slower? Over 1M iterations yours takes 1200ms on my machine. The other takes 700ms. – Sean Bright Jul 21 '14 at 19:44
  • @SeanBright I made a [jsperf](http://jsperf.com/string-delimiting). If you compile a regex beforehand and reuse it, yours could be faster (but you didn't do that in your answer). Your browser may be optimizing it, though. – royhowie Jul 21 '14 at 19:45
  • Looks like Safari is the culprit, not the regex. Could you update the statement in your answer to reflect that fact please? Thank you. – Sean Bright Jul 21 '14 at 19:47
  • @SeanBright No, it's Chrome's v8 engine; the regex is probably being compiled before the loop and reused. You should change your solution to precompile the regex and reuse it. But, yes, I'll remove that part. – royhowie Jul 21 '14 at 19:49
  • Well the moral of the story is that everyone should be using Chrome :D – Sean Bright Jul 21 '14 at 19:54
  • @SeanBright I don't know. It seems that sometimes regex [is slower](http://stackoverflow.com/a/1145525/2476755) in Chrome too. Once again, having vendor-specific versions of ECMAScript (and the engines on which they run) is screwing us all. – royhowie Jul 21 '14 at 20:01
  • FYI - I did another jsperf test which compiled the regex and it didn't seem to make a difference in the other browsers. – Sean Bright Jul 21 '14 at 20:03
  • @SeanBright I actually only use Safari because of its console layout. I use chrome for everything else. Hopefully, Safari 8 or whatever is being released with OSX10.10 will be as fast as Apple claimed (supposedly better than Chrome). I'm dubious, though. – royhowie Jul 21 '14 at 20:08