Replace String with value `$&' not working in javascript [duplicate] - javascript

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 :)

Related

how to get second value from string in JavaScript? [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 4 months ago.
I have a string with comma now i want to get first value how can i get it?
My Code:-
var data = "0.46609362959861755, 0.25069287419319153, 0.5107838958501816, 0.26014574989676476";
console.log(data.replaceAll(',','').splice(0,2));
Thanks for your efforts!
Maybe you need this?
data.split(',')[0]
console.log(data.split(', ')[0])
Use this instead of what you did.
data.split(', ')
returns an array with every element in your string, separately, with no spaces or commas attached. The [0] after it grabs the first element of the array.

Regex how to get what comes after a certain word in a string? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 2 years ago.
http://localhost:3000/?code=85b1a3645tgreg1f54221d8d9f54923b88ade29945yttrtgdg903
I am trying to get whatever comes after 'code=' from this string. How could I do this?
You could use
code=(.+)
See a demo on regex101.com.
In JavaScript this could be:
let string = 'http://localhost:3000/?code=85b1a3645tgreg1f54221d8d9f54923b88ade29945yttrtgdg903';
let m = string.match(/code=(.+)/);
console.log(m[1]);
But it would possibly be more straight-forward to parse the url as it is and use the query part accordingly.
str.match(/code=(.*)/)[1] will do that for you.

Replace quote sign in JavaScipt [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 2 years ago.
I got following code snippet, it's pretty simple,
var b = "'aa','bb'";
console.log(b.replace("'", ""));
// result is "aa','bb'"
I want replace all single quote signs with blank. So my expected output should be "aa,bb", but the actual output is "aa','bb'" neither run this code snippet in Node nor browser. Seems only the first single quote sign has been replaced.
I already got a workaround to resolve this problem by replace with regex. What I wanna know
is what happened to replace function here? I cannot figure this out.
Try using RegEx specifying the global flag (g) that matches the pattern multiple times. Please also note that, as replace() does not modify the original string you have to reassign the modified value to the variable:
var b = "'aa','bb'";
b = b.replace(/'/g, "");
console.log(b);

Javascript regex that escapes \ sign [duplicate]

This question already has answers here:
Remove all backslashes in Javascript
(4 answers)
Closed 4 years ago.
I am trying to convert string that has following values
"A\"s\"sets"
my goal is to remove from string \ values no matter how many of them appear in string.
"A"s"sets"
I tried using new RegExp but I do not manage to perform that operation.
I even managed to create regex that will pick up everything except \ sign
[a-zA-Z0-9'"*]
I also tried calling on
regex.exec(string)
but I am getting an array instead of cleared string.
Anyone have any idea how to do this ?
Thank you
You can use replace.
let str = `"A\"s\\"sets"`
let op = str.replace(/\\+/g, '')
console.log(op)

Javascript character replace all [duplicate]

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.

Categories

Resources