Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace everything but a specified string using regex

I've been looking through and out of stackoverflow for this, but I haven't had any luck.

The string I want to work with is "xbananay", where 'x' and 'y' can be any random combination of letters or numbers with any length. So my string could simply be "qrstbananag", but it could also be "abcbanana12345" for example.

I want to use, and only use, javascript's replace function to replace everything BUT "banana". I already have some regex that can find banana, but the replace function, as intended, will replace what I am looking for, when I want to find everything else. Example:

var fullString = "qrstbananag"
var strippedBanana = fullString.replace(/(?:banana)/g, ''); //returns qrstg

I also have a regex expression that ALMOST gets me what I am looking for, but includes all characters in the string "banana". Another example:

var fullString2 = "abcbanana12345"
var strippedBanana = fullString2.replace(/[^(?:banana)]/g, ''); //returns abbanana

How might I accomplish this using only the replace function? Thanks in advance.

like image 800
ddantas Avatar asked Jun 30 '16 20:06

ddantas


People also ask

How do you match everything except a word in regex?

How do you ignore something in regex? To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.

How do you replace all occurrences of a regex pattern in a string?

sub() method will replace all pattern occurrences in the target string. By setting the count=1 inside a re. sub() we can replace only the first occurrence of a pattern in the target string with another string. Set the count value to the number of replacements you want to perform.

How do I replace a word in a string in regex?

To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring. The string 3foobar4 matches the regex /\d. *\d/ , so it is replaced.


1 Answers

You could use this:

var test = 'abcbananadefbananaghi';

var result = test.replace(/(banana)|[^]/g, '$1');

console.log(result);

The thing is to match banana and put it back in the result with $1. When banana doesn't match, the next character ([^], which also matches newlines) is not captured in a capture group, and so does not get included in the $1.

Instead of [^] you can also use [\S\s]: same behaviour.

About classes

In your second attempt you used

[^(?:banana)]

But be aware that the characters in a class (between [...]) are treated more literally than they would otherwise. So (?: is treated as three separate characters, and so are the characters of banana. You are in fact saying: remove any character that is not any of these: ()?:abn.

like image 164
trincot Avatar answered Oct 22 '22 10:10

trincot