How to include '\' character in a javascript string [duplicate] - javascript

This question already has answers here:
Escaping backslash in string - javascript
(5 answers)
Closed 8 years ago.
I'm trying to create a string in javascript which needs to be valid JSON the string is
{"query":"FOR u IN Countries RETURN {\"_key\":u._key}"}
I keep going around in circles as to how i can include the \ character given its also used to escape the quote character. can anyone help ?

Escape the backslash itself:
{"query":"FOR u IN Countries RETURN {\\\"_key\\\":u._key}"}
First pair of backslashes represents '\' symbol in the resulting string, and \" sequence represents a double quotation mark ('"').

Let JSON encoder do the job:
> s = 'FOR u IN Countries RETURN {"_key":u._key}'
"FOR u IN Countries RETURN {"_key":u._key}"
> JSON.stringify({query:s})
"{"query":"FOR u IN Countries RETURN {\"_key\":u._key}"}"

Use \\ for double quoted strings
i.e.
var s = "{\"query\":\"FOR u IN Countries RETURN {\\\"_key\\\":u._key}\"}";
Or use single quotes
var s = '{"query":"FOR u IN Countries RETURN {\"_key\":u._key}"}';

Related

String split with different separators JavaScript [duplicate]

This question already has answers here:
Including a hyphen in a regex character bracket?
(6 answers)
Closed 3 years ago.
I am asking you how to split a string using different separators and when the string is empty return just an empty space.
I don't know to combine both.
All I have is:
function split(string) {
var str = string.split(/[+-*]/);
return str;
}
Example:
split("este-es+otro*ejemplo"); // => ["este", "es", "otro", "ejemplo"]
split(''); // => [""]
Thank you.
Move the * at the first position inside square bracket ([]).
If any special character, such as backslash (*) is immediately after the left square bracket, it doesn't have its special meaning and is considered to be one of the characters to match literally.
Try /[*+-]/g
function split(string) {
var str = string.split(/[*+-]/g);
return str;
}
console.log(split("este-es+otro*ejemplo"));

How to replace all / with - from string [duplicate]

This question already has answers here:
Replace forward slash "/ " character in JavaScript string?
(9 answers)
Why this javascript regex doesn't work?
(1 answer)
Closed 4 years ago.
I have a string field 01/01/1986 and I am using replace method to replace all occurrence of / with -
var test= '01/01/1986';
test.replace('//g','-')
but it does't give desire result. Any pointer would be helpful.
You just have a couple issues: don't put the regex in quotes. That turns it into a string instead of a regex and looks for that literal string. Then use \/ to escape the /:
var test= '01/01/1986';
console.log(test.replace(/\//g,'-'))
A quick way is to use split and join.
var test= '01/01/1986';
var result = test.split('/').join('-');
console.log(result);
Note too that you need to save the result. The original string itself will never be modified.

Replace '-' with '--' in a JavaScript string [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I am trying to replace a single dash '-' character in a string with double dashes.
2015–09–01T16:00:00.000Z
to be
2015-–09-–01T16:00:00.000Z
This is the code I am using but it doesn't seem to be working:
var temp = '2015–09–01T16:00:00.000Z'
temp.replace(/-/g,'--')
In JavaScript Strings are immutable. So, when you modify a string, a new string object will be created with the modification.
In your case, the replace has replaced the characters but returns a new string. You need to store that in a variable to use it.
For example,
var temp = '2015–09–01T16:00:00.000Z';
temp = temp.replace(/–/g,'--');
Note The string which you have shown in the question, when copied, I realised that it is a different character but looks similar to – and it is not the same as hyphen (-). The character codes for those characters are as follows
console.log('–'.charCodeAt(0));
// 8211: en dash
console.log('-'.charCodeAt(0));
// 45: hyphen
The hyphen character – you have in the string is different from the one you have in the RegExp -. Even though they look alike, they are different characters.
The correct RegExp in this case is temp.replace(/–/g,'--')
Probably the easiest thing would be to just use split and join.
var temp = '2015–09–01T16:00:00.000Z'.split("-").join("--");

Regex friendly URL [duplicate]

This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 4 years ago.
I'm trying to create a dynamic regex to select URL's based on a segment or the whole URL.
For example, I need to get var.match(/http:\/\/www.something.com\/something/)
The text inside the match() needs to be converted so that special characters have \ in front of them such for example "\/". I was not able to find a function that converts the URL to do this? Is there one?
If not, what characters require a \ in front?
I use this to escape a string when generating a dynamic regex:
var specials = /[*.+?|^$()\[\]{}\\]/g;
var url_re = RegExp(url.replace(specials, "\\$&"));
( ) [ ] ? * ^ $ \ . + | and in your case, / since you're using that as the delimiter in the match.
Further info mostly to pre-empt comments and downvotes: I don't really know where - come from as a special character. It's only special when inside character class brackets [ and ] which you're already escaping. If you want to include characters that are sometimes special (which the OP doesn't) that would include look-ahead/behind characters as well, which include =, < and >.

How to make javascript ignore escape ( \ ) character? [duplicate]

This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 3 years ago.
qAnswersR[90430] = [];
qAnswersR[90430].push("[math]k: \frac{(x+20)^{2}}{256}+\frac{(y-15)^{2}}{81}=1[/math]");
And I need to get the value into variable, but when I console.log out the array like this:
console.log(qAnswersR[90430]);
I get: [math]k: rac{(x+20)^{2}}{256}+rac{(y-15)^{2}}{81}=1[/math],[math]k: 81(x+20)^{2}+256(y-15)^{2}=20736[/math]
But the escape tag "\" disappears, but I need it there, what should I do?
But the escape tag "\" disappears, but I need it there, what should I do?
You need to escape the backslash, i.e., use \\ instead of just \:
"[math]k: \\frac{(x+20)^{2}}{256}+\\frac{(y-15)^{2}}{81}=1[/math]"
^ ^
You can use tagged template literals
var str = (s => s.raw)`[math]k: \frac{(x+20)^{2}}{256}+\frac{(y-15)^{2}}{81}=1[/math]`[0]
The anonymous arrow function will serve as tag and s.raw contains the original input
Escape the escape character, like \\a.

Categories