Replace quote sign in JavaScipt [duplicate] - javascript

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

Related

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

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

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)

Need help to use regex in match function of javascript [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 5 years ago.
I am using a regex for getting some pattern data and it is working fine but I need to use some part of regex from a variable but I am unable to do that please help me if somebody know how to make it.
var subStr = document_root.documentElement.outerHTML.match(/http(.*?Frover.fff.com.*?)\;/g);
alert(subStr[0]);
I get first result in alert but need to do it like this.
var link=rover.fff.com;
var regexpattern="/http(.*?F"+link+".*?)\;/g"
var rex=new RegExp(regexpattern);
var subStr = document_root.documentElement.outerHTML.match(regexpattern);
alert(subStr[0]);
I don't get anything.
Please help me.
Comment if you confuse in anything.
In JavaScript, when you need to use a variable, you have to create a new RegExp object, as you've tried.
It takes two parameters. The first is a string of the pattern (without the slashes at the start and end). The second is any flags (like your g).
So, your pattern would be something like this:
var regexpattern = new RegExp('http(.*?F' + link + '.*?)\\;', 'g')
Note, that you have to escape any special characters in the string like you would any other string (in this case, your \ had to be escaped to \\ so it'll work properly).
After that, you use it the same as the /pattern/ form. In your case, be sure to use rex, not regexpattern in your match() function.
(If so some reason that wasn't a literal \ and you were escaping the semi-colon, just remove it completely, you won't need to escape a semi-colon.)

Need regex to replace entire folderpath while preserving file name where last folder could by anything [duplicate]

This question already has answers here:
How to get the file name from a full path using JavaScript?
(21 answers)
Closed 6 years ago.
I need a regex that would replace something like this but leave the file name.
/folder1/folder2/folder3/anything/somefile.html
Also could someone show me how to implement this with replace method? Replacing the entire path match to empty string and again leaving the file and which would be anything.
Thanks in advance.
You can do it without regular expressions:
var filename = string.split('/').pop();
// "somefile.html"
You can use .*\/.
. will match anything
* will repeat the previous zero or more times.
\/ is a literal slash (/). But needs to be escaped because it's part of the regex construct:
var str = '/folder1/folder2/folder3/anything/somefile.html';
str.replace(/.*\//, ''); // "somefile.html"

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