I would like to do something like this:
s = s.replace(/(”)/g, '"'); // need to replace with double quotes
s = s.replace(/(“)/g, '"'); // need to replace with double quotes
s = s.replace(/(’)/g, "'"); // need to replace with single quotes
But for me this does not work. I tried all the ways.
You can use Unicode values in replace:
s = s.replace(/\u201C|\u201D/g, '"'); // for “ or ”
s = s.replace(/\u2019/g, "'"); // for ’
DEMO: http://jsfiddle.net/uM2fY/
So we open console, and see:
>>> 'test&rqduo;foo'.replace(/&rqduo;/g, '\"' );
test"foo
and with braces:
>>> 'test&rqduo;foo'.replace(/(&rqduo;)/g, '\"' );
test"foo
Everything work just like you thought. Please, check your input strings.
Related
I've got something like this data that comes from API and I need to do JSON.parse this, but the problem was there were inner double quotes so I can't parse it what should I do, can I use regex or something.
const dataString = '{"EN" : "2. This English "Blha Blha" "Woo Woo" something wrong."}';
I also used this regex
replace(/(^"|"$)|"/g, "'");
but by using this regex it's replace all the double quotes with single quotes like this =>
{'EN' : '2. This English 'Blha Blha' 'Woo Woo' something wrong.'};
I only want to replace the quotes like this
{'EN' : '2. This English "Blha Blha" "Woo Woo" something wrong.'};
The thing is that single quotes are not valid symbols for JSON if they are used to wrap key/values. To make it clear - you need to escape double-quote symbols in values. A better approach is to update your API with that correction. More ugly but working way is placed below:
const dataString = '{"EN" : "2. This English "Blha Blha" "Woo Woo" something wrong."}';
const normalize = (dataString) => {
let newString = dataString;
newString = newString.replaceAll(`"`, `\\\"`);
newString = newString.replaceAll(`{\\\"`, `{"`);
newString = newString.replaceAll(`\\\"}`, `"}`);
newString = newString.replaceAll(`\\\" : \\\"`, `" : "`);
return newString;
};
console.log(JSON.parse(normalize(dataString)));
I am recieving strings like:
"\\r\\n "
"\\r \\n"
"\\n \\r "
etc from a third party API.
I need to convert these to empty string before storing them into DB. How can I properly sanitize these in Node.js?
I can use .replace but I want to make sure to catch all edge cases.
Considering you just want to replace the string with no values other than space character class or escaped space characters.
let str = `"\\r\\n "
"\\r \\n"
"\\n \\r "
"\\n \\r Hello"
`
let op = str.replace(/^"(?:\\r|\\n|\s)*"?$/gm,'')
console.log(op)
Try this :
console.log("\\n \\r ".replace(/\\(n|r)\s*/g,""));
Try ( i use \\\\r for slashesh as escape characters in JS to poperly generate your input string - which is shown console)
let input=" \\\\r \\\\n ";
let output= input.replace(/\s*(\\\\r|\\\\n)\s*/g, '');
console.log(`input : "${input}"` );
console.log(`output : "${output}"`);
I have an HTML element like this
<div id='test' onclick='window.location="http://example.com/";'></div>
I'm annoyed because I would like to put this into a string but I'm not able since this element use both ' and " in its syntax.
How can I store such syntax into a string?
Try This:
var data = "<div id='test' onclick='window.location=\"http://example.com/\";'></div>"
alert(data)
Just escape the strings with a \.
var myString = "<div id='test' onclick='window.location=\"http://example.com/\";'></div>";
escape the ' or " with a backslash
var testString = '<div id=\'test\' onclick=\'window.location=\"http://example.com/\";\'></div>';
alert(testString);
you can use backslash \ for putting special character in string
for example for storing string "(double quotes) and '(single quotes) you can write
var s="\"\'"
How can I convert this 2016-01-04 into this 2016-01-04 in JavaScript?
I have a dataset in an array with dates like this:
["x", "2016-01-04", "2016-01-05", "2016-01-06", "2016-01-07", "2016-01-08", "2016-01-09"]
And I want to covert them to:
['x', '2016-01-04', '2016-01-05', '2016-01-06', '2016-01-07', '2016-01-08', '2016-01-09']
I have tried .replace(/"/g, "'")
but I get an error forcastDate_ordered.replace is not a function
See Special Characters (JavaScript) from MSDN
Characters like the speech mark " and single quotation mark ' can be escaped with a backslash \ - this is helpful when they are in strings using speech marks or quotation marks around them.
And to replace the characters, use String.replace
So the final answer as others have stated is
string s = "\"2016-01-04\"";
return s.replace("\"", "'");
(The single quote is not escaped because the string is surrounded by speech marks " - so it doesn't need it)
UPDATE: your question is changed to involve arrays
In which case, you need Array.map
array.map(s => s.replace("\"", "'"));
var endString = startString.replace(/"/g, "'");
Example:
var startString = 'I hate "double" quotes';
var endString = startString .replace(/"/g, "'");
endString = I hate 'double' quotes
or
doubleQuotesReplacer = (startString) => startString .replace(/"/g, "'");
I have this url that I have to pass as a javascript string. I know that I need to escape the characters, but no matter what I do I can't seem to get it right?
var v = "www.youbigboy.com/showthread.php?t=1847";
You are using double quotes inside a string declared with double quotes. You can:
Change the double quotes inside the string to single quotes
var v = "<a href='www.youbigboy.com/showthread.php?t=1847'>www.youbigboy.com/showthread.php?t=1847</a>";
Escape the double quotes inside the string with \
var v = "www.youbigboy.com/showthread.php?t=1847";
Change the double quotes outside the string to single quotes
var v = 'www.youbigboy.com/showthread.php?t=1847';
you can use single quotes ' to differentiate internal quotes, or you can escape your internal quotes \"
The code becomes:
var v = "<a href='www.youbigboy.com/showthread.php?t=1847'>www.youbigboy.com/showthread.php?t=1847</a>";
or
var v = "www.youbigboy.com/showthread.php?t=1847";
maybe this
var url = "www.youbigboy.com/showthread.php?t=1847"; //or other dynamic url's
var sb = "<a href='" + url + "'>big boy url</a>";
source: is the + operator less performant than StringBuffer.append()