0

Why does this works:

'ye+low'.replace(/\+/g, 'l')
// > yellow

but this does NOT work:

'ye\low'.replace(/\\/g, 'l')
// > yelow

??

I need to replace ONE backslash with something, but I can't seem to make it happen.

NOTE: I CAN'T change the string as it comes in a variable.

EDIT: I understand \ is an escape character in javascript. This is fine with my understanding and I read plenty of other SO answers in this regard. My question is: "Ok I know, but still: HOW DO I REPLACE ye\low to be yellow using javascript?" I understand regex may not be the way to go because of its interpretation of backslashes, but I bet there is some way to get the desired output i some fashion.

Jona Rodrigues
  • 992
  • 1
  • 11
  • 23
  • https://stackoverflow.com/questions/4025482/cant-escape-the-backslash-with-regex – Jonas Wilms Oct 09 '17 at 21:43
  • Sorry it is not relevant. It just explains why backslashes are treated differently. I need a way to turn `ye\low` to `yellow`. – Jona Rodrigues Oct 09 '17 at 22:05
  • `'ye\low'` does not contain any blackslash. Use console.log('ye\low') to confirm this. `'ye\low'` is not valid and is then converted to just `'yelow'` without the slash. See my answer below for explanation to why. – joelnet Oct 09 '17 at 22:16
  • @JonaRodrigues I think it might help if you put this into context. Is it only the word `ye\low` literally your only concern? Is it a limited group of words instead? Where is this data from? – zer00ne Oct 09 '17 at 22:31
  • @zer00ne Actually this a bit off context: the `ye\low` string is actually a RegEx that someone gives me. With this RegEx, I am supposed to create a `new RegExp(userRegex, 'g')`. Now after several readings, I found that `userRegex` should be escaped in order to be correctly turned into a regex object. So I am now fighting to escape the escape character `\\`. I thought I was doing a good job by narrowing down the problem to a shorter problem, but it seems to cause more confusion... – Jona Rodrigues Oct 09 '17 at 22:41
  • Unfortunately, I can't go to the end user and tell him "please double each of your backslashes for me"... As I understand they use their regex for other purposes and their job is not toward modifying regexes manually (neither does mine...) This is why, I try to find a way to escape `\\` using javascript and not me doing the job by hand. – Jona Rodrigues Oct 09 '17 at 22:45
  • @JonaRodrigues Ah, then I think I know what you need to overcome this escaping issue. See my answer. – zer00ne Oct 09 '17 at 22:47
  • If you want to represent a backslash in code, you have to escape the backslash like this `'ye\\low'`. This might look like two backslashes, but this is the code that represents ONE backslash. – joelnet Oct 09 '17 at 23:59
  • Please see the demo provided in my answer to better demonstrate how to solve this. – joelnet Oct 10 '17 at 00:08

2 Answers2

2

You code shows \l, which is not 2 characters, but one character. It is an invalid escape code that falls back to just l. If you want to represent a backslash in code, you have to escape the backslash like this 'ye\\low'. This might look like two backslashes, but this is the code that represents ONE backslash.

This is a string of 5 characters: 'ye\low'.

console.log('ye\low')
// "yelow"

'ye\low'.length === 5

These two blocks of code are identical:

'ye\low'.replace(/\\/g, 'l')
'yelow'.replace(/\\/g, 'l')

The character \l is invalid and is translated to an l with no slash.

If your string has a slash in it, you have to escape the backslash like this: 'yel\\low'

const yelloWith_ONE_Backslash = 'ye\\low'

console.log(yelloWith_ONE_Backslash)
// "ye\low"

'ye\\low'.length === 6
// true

console.log('yelow')
// "yelow"

console.log('ye\low')
// "yelow"

console.log('ye\\low')
// "ye\low"

console.log('ye\\\\low')
// "ye\\low"

So you would do this:

'ye\\low'.replace(/\\/g, 'l')

Demo

var input = prompt('Try to type `ye\\low`')
var replaced = input.replace(/\\/g, 'l')

alert(replaced)
Community
  • 1
  • 1
joelnet
  • 13,621
  • 5
  • 35
  • 49
  • Thanks for your prompt reply. But I don't have access to the variable... I can't replace it manually before computing the `replace()` function: that's actually why I use the replace function. If I could modify it manually, I would change it to 'yellow' directly... ahah – Jona Rodrigues Oct 09 '17 at 21:50
  • 1
    You would not be doing a replace. `var yelow = 'ye\\low' // > yel\low`. Try to output `'ye\\low'` and you will see there is only 1 slash in the actual string, even though the code will contain 2. – joelnet Oct 09 '17 at 21:54
  • `'ye\low'` is not valid. The value can only be either `'yelow'` or `'ye\\low'`. You can check this by running `'ye\low' === 'yelow' // true`. `.replace(/\\/g, 'l')` will work. It's your test code (`'ye\low'`) that is wrong. – joelnet Oct 09 '17 at 21:58
  • thanks for your lights. Anyway, I unfortunately have this misleading `ye\low` string to be changed to `yellow`... I don't know what to do about that, there should be some hack to replace that using a computer and some instructions, right ? – Jona Rodrigues Oct 09 '17 at 22:03
  • 1
    I have updated my answer to help. The value `'\\'` represents a single character that is a blackslash. What you are suggesting not possible. `.replace(/\\/g, 'l')` will work for you. – joelnet Oct 09 '17 at 22:06
  • Open up your developer console and type this into it `'ye\low'`. You will see the value of that is `'yelow'`. If you are looking for a value of `'ye\low'`, the JavaScript you must write will be `'ye\\low'`. – joelnet Oct 09 '17 at 22:09
  • Yes, I understand that. The point is `ye\low` comes from a user input. I can't instruct him to double its backslashes because I can't program it. So what would be the best way to tell javscript that for this string, the `\\` should not be an escape character ? – Jona Rodrigues Oct 09 '17 at 22:16
  • Another way to see the problem would be: how would I make `ye\low` to be `ye\\low` then ? – Jona Rodrigues Oct 09 '17 at 22:19
  • When the user enters his input as `ye\low` and we set it to `myVar` you would test it with `myVar === 'ye\\low' // > true`. Please re-read and test the code I have suggested. – joelnet Oct 09 '17 at 22:19
  • You can confirm this by entering this into your browsers console `alert(prompt().replace(/\\/g, 'l'))`. When prompted, enter the value `ye\low`. – joelnet Oct 09 '17 at 22:22
  • If the user enters the value `ye\\low`, you would test that with `input === 'ye\\\\low'` `\\` is one single backslash. – joelnet Oct 09 '17 at 22:25
  • Sorry, this does not fit my needs. I just have this string with ONE backslash stuffed in it. I am not the proprietary of the input-side code... I just get `ye\low` and need to convert it to `yellow`. Nothing less, nothing more. – Jona Rodrigues Oct 09 '17 at 22:31
  • This absolutely does fit your needs. THIS IS THE ANSWER! `'ye\low'` contains ZERO backslashes. `'ye\\low'` contains ONE backslash. You can confirm this by entering this into your browsers console `alert(prompt().replace(/\\/g, 'l'))`. When prompted, enter the value `ye\low` – joelnet Oct 09 '17 at 23:28
  • Sorry but read the question and description... I CANNOT MODIFY `ye\low` to `ye\\low` manually. If your answer involves modifying the original `ye\low` string, then you are not replying to my question. I don't mean to be rude or what, I appreciate your explanations sincerely and you helped me find the correct answer. Now, I can't upvote an answer telling me to change something I explicitly told I don't have hands over... Sorry. My own amswer does the trick, as it allows me to replace `ye\low` WITHOUT doing it manually, but using javascript instead. – Jona Rodrigues Oct 10 '17 at 02:00
  • Okay, let's back up a bit. How is this JavaScript file being created? How is the user input being received? I believe there is a key element being left out of this problem. Is the user input assigned to a variable? something similar to this `var input = prompt('enter text')`? Can you please post more of your code because `'ye\low'` is not a valid string literal in JavaScript. That becomes translated to `'yelow'`. So there is never a slash in the literal. – joelnet Oct 10 '17 at 09:12
0

I found this to work for me:

// This allows backslash to be ineffective, meaning ye\low will remain as a string with 6 characters INCLUDING the \
var value = String.raw`ye\low`;
console.log( value.replace('/\\/g', 'l') )

Output is

yellow

To use with caution as it is not widely supported by all browsers yet. See more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw

Jona Rodrigues
  • 992
  • 1
  • 11
  • 23
  • `String.raw\`ye\low\` === 'ye\\low'`. These two are *triple equals identical*. `'ye\\low'` contains only ONE backslash. Please re-read my original answer to understand why. `if (String.raw\`ye\low\` === 'ye\\low') { console.log('ALWAYS TRUE!'); }` – joelnet Oct 09 '17 at 23:37
  • I have modified my answer to include a demo to PROVE to you this is the answer you are looking for. – joelnet Oct 10 '17 at 00:08
  • Thanks @joelnet. Like I said, you are right: but you are not replying to the original question which states: `NOTE: I CAN'T change the string as it comes in a variable.` which means what it means: I can't put my mouse between `ye` and `\low` and add something here. I just can't... So I need a solution that STARTS with the string `ye\low`. If any solution involves touching the `ye\low` string by other means than javascript... then the answer is out of scope. Using `String.raw` I leave the original string/sequence untouched and the `replace()` finally matches the `\\`. – Jona Rodrigues Oct 10 '17 at 02:09
  • Can you please post more of your code? I would like to see what you mean by "it comes in a variable". Specifically, I am looking for how the user's input gets assigned to the variable. Because the Demo I provided gets the user's input, assigns it to a variable and then replaces the slash with an l. Did you run the Demo in my example? – joelnet Oct 10 '17 at 09:14