Find and Replace Backslash in a JavaScript String

JavaScript Question

This is I think a "pure" JavaScript question not DO really hence posting in Off-Topic.
It is related to this post:
Rename command, {alias} code and Double backslashes

Objective

In a JavaScript string variable I have reason to want to replace all instances of backslash

\

With TWO instances of (the same) backslash:

\

How do I do this in JavaScript in DO?

Problem

My problem is that the BackSlash is a Regex active Character

Control Method

If I want to replace the dash (-) with two dashes (--) that is easy.
I do this successfully in 2 ways as shown in the green and blue boxes below

  1. Green box = Use the String.replace method
  2. Blue box = Use the String.split method to create an array then the Array.join method to rejoin with the added character (dash in this case)

Broken Method

This is shown in the red box which as you can see tries the String.replace method.
It does not help that the backslash character is not a literal in a Javascript string either.
If this method did work then we should see the string presented with a set of single backslashes (as the second backslash would get eaten up acting as an escape character). But it does not.

Any ideas? Much appreciate any help......
PS just looked through the new feature summary for DO13..... very interesting!!!!

No, you probably do not :slight_smile:


You are not entering backslashes in line 19, you are just escaping a few characters that don't need it. \b is the same as b. That is the explanation for lines 20 and 21, not regex.

Remember: To write a backslash to a javascript string you need to escape it: \\. The string will then contain one backslash.

One, only one. A single backslash. Not two. Just ONE. :one:

@ixp where would I be without you?
I understand what you are saying.
But I need to think. Hard :slight_smile: