删除彼此相邻的多次出现的字符串--javascript或jquery

时间:2021-04-24 07:38:07

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

我需要删除任何| str |发生在另一个| 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.

换句话说,如果有很多| str |彼此相邻,我们删除除了一个之外的所有。

2 个解决方案

#1


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

#2


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:

另一种简单的方法是.split()基于你计划使用的分隔符的字符串,非空元素的.filter(),然后是具有相同分隔符的.join():

JavaScript

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

这允许您避免为每个案例制作特定的正则表达式 - 没有转义字符 - 而是通过.split()和.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.

不确定此解决方案是否更快。在非Chromium浏览器中似乎更快。我猜测Chrome是有效的,因为v8引擎,在运行它之前编译正则表达式一百万次,使得正则表达式解决方案更快。

fiddle

#1


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

#2


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:

另一种简单的方法是.split()基于你计划使用的分隔符的字符串,非空元素的.filter(),然后是具有相同分隔符的.join():

JavaScript

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

这允许您避免为每个案例制作特定的正则表达式 - 没有转义字符 - 而是通过.split()和.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.

不确定此解决方案是否更快。在非Chromium浏览器中似乎更快。我猜测Chrome是有效的,因为v8引擎,在运行它之前编译正则表达式一百万次,使得正则表达式解决方案更快。

fiddle