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.

9
  • stackoverflow.com/questions/4025482/… Commented Oct 9, 2017 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. Commented Oct 9, 2017 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. Commented Oct 9, 2017 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? Commented Oct 9, 2017 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... Commented Oct 9, 2017 at 22:41

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

15 Comments

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
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.
'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.
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 ?
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.
|
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

4 Comments

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!'); }
I have modified my answer to include a demo to PROVE to you this is the answer you are looking for.
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 `\`.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.