最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to replace everything but a specified string using regex - Stack Overflow

programmeradmin7浏览0评论

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.

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.

Share Improve this question edited Jun 30, 2016 at 20:18 ddantas asked Jun 30, 2016 at 20:11 ddantasddantas 1271 silver badge9 bronze badges 2
  • 4 what is the benefit of such replacement? Why not just check the occurrence of banana within some word and replace that word with banana ? – RomanPerekhrest Commented Jun 30, 2016 at 20:23
  • I had the same question as @RomanPerekhrest. – conner.xyz Commented Jun 30, 2016 at 20:25
Add a comment  | 

1 Answer 1

Reset to default 18

You could use this:

var test = 'abcbananad\nefbananaghi';

var result = test.replace(/(banana)|./gs, '$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 due to the s flag) is not captured in a capture group, and so does not get included in the $1.

NB: Before the s flag was supported, you could use [^] or [\S\s] instead of ..

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.

发布评论

评论列表(0)

  1. 暂无评论