Delete line between substring from RegExp

I am writing this code to remove a string from 'a' to 'c'

var str = "abcbbabbabcd";
var re = new RegExp("a.*?c","gi");
str = str.replace(re,"");
console.log(str);

The result in the console is "bbd"

But the result that suits me is "bbabbd"

Can regex be used for this problem?

Thanks for the help.

+4
source share
4 answers
a(?:(?!a).)*c

Use regex.exe. .*?. .*?will consume aalso after the first a. To stop it, use lookahead.

https://regex101.com/r/cJ6zQ3/34

EDIT:

a[^a]*c

In fact, you can use negated character classas you have only 1 character.

+2
source

g regexp, , abc abbabc. , a..c. , abc abbabc. bbd , " " a "" c ".

abcp > bb abbabcp > d => bbd

+1

You need to update your regular expression to , this will avoid the character and between and a[^ac]*?cacac

var str = "abcbbabbabcd";
var re = new RegExp("a[^ac]*?c","gi");
str = str.replace(re,"");
console.log(str);
Run codeHide result

Regular expression visualization

0
source

Here is another way.

var str = "abcbbabbabcd";
var str= str.replace(/abc/g, ""); 
console.log(str);
0
source

Source: https://habr.com/ru/post/1610688/


All Articles