0

I need a little help in trying to sanitize a string. I have written a regular expression that is pretty close to giving me the results I want but I just can't quite get it right. The string I'm receiving is in this format. ||a|assa||asss||ssss The pipe character are basically placeholders to what would have been a separator for text. However, I'm trying to end up with something that would look like this. |a|b|c|d in other words I'm just trying to remove consecutive pipes. I have put together a little example to illustrate what I have attempted and keep failing miserably.

const str1 = "||a||jump|fences||in the street";
const str2 = "im a wolf";
const hasPipe = /\|{1}\+/;//if the | is consecutevely repeated more than once than deleted.

console.log(hasPipe.test(str1));
console.log(str1.replace(hasPipe, ""))
console.log(hasPipe.test(str2));

The expected result to the above code should simply be. |a|jump|fences|in the street" Can someone please point me in the right direction or point my silly mistake.

3
  • 2
    You're completely misunderstanding regex. To repeat, you just want +. Commented May 25, 2018 at 17:46
  • I'm pretty certain I've tried that variation and was not able to get the results correctly. I'll give it another shot. so should it be these ? /\|\+/ because that does not seem to work? Commented May 25, 2018 at 17:47
  • 1
    Why are you escaping the +? Read the documentation and learn how regex works. Commented May 25, 2018 at 17:55

4 Answers 4

2

Given your test string const str1 = "||a||jump|fences||in the street"; you want to replace multiple occurrences of pipe | with a single pipe.

There are a couple of ways to match a non-empty sequence:
+ = match 1 or more of the previous expression
{n,m} = match at least n but not more than m occurrences.
{n,} = match at least n and unlimited times.

Simple:

str1.replace(/\|+/g, "|")
"|a|jump|fences|in the street"

Matches one or more pipes and replaces with a single pipe. This replaces a single pipe with a pipe.

More exact:

str1.replace(/\|{2,}/g, "|")
"|a|jump|fences|in the street"

Matches two or more (because there is no max after the comma) pipes and replaces with a single pipe. This does not bother replacing a single pipe with another single pipe.

There are also a couple of ways to match exactly two pipes, if you'll never have a run of 3 or more:

str1.replace(/\|\|/, "|");
str1.replace(/\|{2}/, "|");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Stephen for the explanation. I have been added for a few hours now and I could not grasp why it was not working. I'm sincerely humbled and appreciative.
2

Not much to it:
\|\|+ replace with |

https://regex101.com/r/vvkrI0/1/

Comments

2

You can use the + to find all the locations that have 1 or more pipes in a row, and replace them all with a single pipe. Your regex would simply be:

/\|+/g

Here is an example, with a variable number of pipes:

const str1 = "||a|||jump|fences||||in the street";
var filtered_str1 = str1.replace(/\|+/g,"|")
console.log(filtered_str1);

Comments

1

You could substitute consective pipe characters like this:

const pat = /\|{2,}/gm;
const str = `||a|||jump|fences||in the street`;
const sub = `|`;

const res = str.replace(pat, sub);
console.log('result: ', res);

Result:

|a|jump|fences|in the street

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.