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

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.

Related

Replace certain strings globaly [duplicate]

This question already has answers here:
How to escape regular expression special characters using javascript? [duplicate]
(3 answers)
Closed 2 years ago.
Sorry, this sounds very basic, but I really can't find on Google.
In order to replace contents in a string globally, you can use things like...
a.replace(/blue/g,'red')
But sometimes you need to replace characters that's not compatible with the example above, for example, the character ")"
So, this will fail...
const a ="Test(123)"
a = a.replace(/(/g, '')
VM326:1 Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group
How to replace string of characters like that ?
at :1:7
The special regular expression characters are:
. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
const a ="Test(123)";
console.log(a.replace(/\(/g, ''));
you need to use the escape char \ for this. there are set of char which need to be escaped in this replace(regex)
a.replace(/\(/g, '');
Find a full details here at MDN
you need to escape the ( with \ in a new variable because a is const
and it will work
var b = a.replace(/\(/g, '');
for more practicing use this site
regExr

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.

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

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}"}';

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

Javascript replace doesn't work when it should [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 7 years ago.
var src = "http://blah.com/SOMETHING.jpg";
src.replace(/.*([A-Z])\.jpg$/g, "X");
at this point, shouldn't src be:
http://blah.com/SOMETHINX.jpg
If I use match() with the same regular expression, it says it matched. Regex Coach also shows a match on the character "G".
Try
src = src.replace(/.*([A-Z])\.jpg$/g, "X");
String#replace isn't a mutator method; it returns a new string with the modification.
EDIT: Separately, I don't think that regexp is exactly what you want. It says "any number of any character" followed by a captured group of one character A-Z followed by ".jpg" at the end of the string. src becomes simply "X".
The replace function doesn't change src.
I think what you want to do is:
src = src.replace(/.*([A-Z])\.jpg$/g, "X");
src.replace will replace the entire match "http://blah.com/SOMETHING.jpg", not just the part you captured with brackets.

Categories