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.
ye\lowtoyellow.'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.ye\lowliterally your only concern? Is it a limited group of words instead? Where is this data from?ye\lowstring is actually a RegEx that someone gives me. With this RegEx, I am supposed to create anew RegExp(userRegex, 'g'). Now after several readings, I found thatuserRegexshould 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...