Javascript regex that escapes \ sign [duplicate] - javascript

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)

Related

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

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

how to parse values from comma separated values using regex for javascript [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 years ago.
I have
HOSTNAMEA,HOSTNAMEB,HOSTNAMEC,...
I have a third party workflow tool that can do the looping but can only use regex to parse values. I'd like to get a regex that grabs each hostname and puts into it's own variable in my workflow tool so the results will be
HOSTNAMEA
HOSTNAMEB
HOSTNAMEC
...
I'm struggling to get a regex that just grabs the text block X between the commas
ever heard of \w+ if you just want the strings between the comma, you can use .split(", ") as well
var str = "HOSTNAMEA,HOSTNAMEB,HOSTNAMEC";
var res = str.match(/\w+/g);
console.log(res.join(" "));
sample code for your help

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.

How to replace four backslashes in a string to two [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I have a string like this coming from the server and its not working due to four backslashes. if i remove four with two its working.
URL_https~~\\\\fbcdn-sphotos-f-a.akamaihd.net\
May I know how to replace four backslashes with two as below
URL_https~~\\fbcdn-sphotos-f-a.akamaihd.net\
I tried various things but nothing worked out
i tried as follows
one:
strTest2.replace("\\\\\\\\","\\\\"
two:
strTest2 .replace(/[/\*]/, "");
Three:
strTest2.replace(/\|\|/g, "\\");
You need to store the new string created
strTest2 = strTest2.replace("\\\\\\\\","\\\\");
all of the replace methods return a new string. not alter the current string.
You need to assign the result, because strings are immutable.
The first one would have actually worked, but it only replaces the first occurrence of four backslashes. To replace all occurrences, you need to use an actual regex literal:
strTest2 = strTest2.replace(/\\\\\\\\/g,"\\\\");
You can improve readability of the above expression with a quantifier:
strTest2 = strTest2.replace(/(?:\\){4}/g,"\\\\");

Categories

Resources