Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Javascript non-capturing group in a replace

The expression

"abcb".replace(/(?:a)b/, 'x')

returns "xcb". What I want it return is "axcb"; that is, the "a" is not captured.

Is this possible in a single regex?

like image 586
Michael Lorton Avatar asked Feb 19 '26 20:02

Michael Lorton


1 Answers

You can make it capturing:

"abcd".replace(/(a)b/, '$1x')
//=> axcd
like image 91
anubhava Avatar answered Feb 21 '26 11:02

anubhava