Please enable JavaScript to use regex101
/
^
r
e
g
e
x
[
\
-
_
]
?
1
0
1
$
/
i
Reg
ular
Ex
pressions
101
Upgrade to Pro
Enterprise
Regular Expression
/
(?:\G(?!^)|string1)(?:(?!string1|string2).)*?\K\h(?=.*string2)
/
gm
Input 1
1
New
string1 this is a first example string2 string1 this is a second example string2
Substitution
_
string1 this is a first example string2 string1 this is a second example string2