Javascript regex replace inserts extra slashes -
i'm trying clean file using javascript. file contains lines of text this:
a <- b + c / d;
i want replace <-
=
when there string of non-whitespace on either side of <-
, separated single space. pretty easy in theory:
line = "a <- b + c / d" result = line.replace( /(\s+) <- (\s+)/, /$1 = $2/ )
the above code produces /a = b/ + c / d
when run. however, conceptually, should produce a = b + c / d
. how can use $1
-style backreferences without allowing javascript opportunity insert slashes willy-nilly?
use string second parameter:
result = line.replace( /(\s+) <- (\s+)/, "$1 = $2" );
.
what happening second parameter being coerced regexp
string
adds slashes in replacement.
Comments
Post a Comment