This question already has answers here:
`string.replace` weird behavior when using dollar sign ($) as replacement
(3 answers)
Closed 8 years ago.
At following String.replace line, lossLocation.state$ ,
the $ sign is removed after the replace,
i need to keep the $, as it is used in the variable.
'{0}'.replace(
'{0}'
, 'categories.Country === \'$formData.lossLocation.state$\'.toUpperCase()')
It gives me
"categories.Country === '$formData.lossLocation.state'.toUpperCase()"
The expected outcome should be
"categories.Country === '$formData.lossLocation.state$'.toUpperCase()"
i've tried the following but still been removed when replace
state\$
As it states in String.prototype.replace(). To escape '$' in replacement content, you should use '$$' instead of '\$'.
So a proper way of constructing it would be
'{0}'.replace('{0}',
'categories.Country === \'$formData.lossLocation.state$\'.toUpperCase()'
.replace(/\$/g, '$$$$')
)
Related
This question already has answers here:
Why 'ABC'.replace('B', '$`') gives AAC
(2 answers)
Closed 1 year ago.
I have string show {{value}} and I want replace {{value}} with $& but it not work. It return current value show {{value}}.
Here is my code
let data ="show {{value}}";
let output = data.replace("{{value}}","$&");
alert(output);
I don't know why it not work. I try replace with other strings same $1, $a and it work.
How I can fix my problem
$ is a special symbol in javascript. Write $$& instead and it should work :)
This question already has answers here:
AJAX POST and Plus Sign ( + ) -- How to Encode?
(6 answers)
Closed 5 years ago.
I have a web app where if someone selects something in the dropdown menu, it changes the next field with Ajax. I'm having difficulty when the values of the dropdown have a '+' symbol which breaks it.
For example this works:
if ($_GET['ch'] == 'Something here - here') {}
However this does not
if ($_GET['ch'] == 'Something here + here') {}
I'd like a solution to be able to include the + symbol inside. Some symbols seem to work fine including brackets (), dashes -, etc.
Try encodeURI function, and/or use POST instead.
Also escaping characters would be good. (like \+ instead of +)
When you are escaping characters than at php side you should use stripslashes function if you need special characters.
This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 5 years ago.
Is there any way to make something.+()[]* matching literally 'something.+()[]*'? I'm using regex builder so manual escaping is not allowed. Sure, i can add hardcoded checks if (char === '+') return '\+' but i'm looking for native solution or better way
UPD
I'm sorry. I forgot to add that matching should be in given order with moving forward but not back. So [+.] will not fit my requirements because it will match both +. and .+. I need only first case (In definition order)
You don't need to escape them if within square brackets.. I just tested and works for me, but maybe not what you are looking for?
something[.+()[]]
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 7 years ago.
I am trying to find all the characters ('?') of a URL and replace it with &.
For instance, i have var test = "http://www.example.com/page1?hello?testing";
I first attempted:
document.write(test.replace("&","?"))
This resulted in that only the first ? would be replaced by & , then I found a question saying that I could add a g(for global)
document.write(test.replace("&"g,"?"))
Unfortunately, this did not have any effect either.
So how do I replace all characters of type &?
You need to escape the ? character like so:
test.replace(/\?/g,"&")
You're almost there.
the thing you saw in SO is regex replace :
document.write(test.replace(/\?/g,"&")) ( I thought you wanted to change & to ? , but you want the opposite.)
with the G flag - it will replace all the matches in the string
without it - it will replace only the first match.
This question already has answers here:
JavaScript regex multiline text between two tags
(5 answers)
Closed 4 years ago.
Even if I use the m flag, javascript regex seems to isolate regex matching by lines.
Example:
"if\nend".match(/if(.*?)end/m)
=> null
I want this to match. How do I get around this?
You actually want s (a.k.a. "dotall"), not m, but javascript doesn't support that. A workaround:
"if\nend".match(/if([\s\S]*?)end/)