unterminated string literal in the variable - javascript

var MM = '\' + obj[0]['MM '] + '/';
I get two errors while using this code...
missing; before statement and
unterminated string literal

The character \ is "special" because it's used to allow the use of all printable characters in strings. In your case '\' is not a string composed by the only character \, but the beginning of a string starting with the single quote character '.
For exampe if you want the string Hello Andrea "6502" Griffini you can use single quotes
string1 = 'Hello Andrea "6502" Griffini';
and if you want single quotes in the string you can do the opposite
string2 = "Hello Andrea '6502' Griffini";
But what if you want both kind of quotes in the same string? This is where the escape \ character comes handy:
string3 = "'llo Andrea \"6502\" Griffini";
Basically \ before a quote or double quote in a string tells javascript that the following character is just a regular character, with no special meaning attached to it.
Note that the very same character is also used in regular expressions... for example if you want to look for an open bracket [ you must prefix it with a backslash because [ in a regular expression has a special meaning.
The escape is also used to do the opposite... in a string if you put a backslash in front of a normal character you are telling javascript that that character is indeed special... for example
alert("This is\na test");
In the above line the \n sequence means a newline code, so the message displayed will be on two lines ("This is" and "a test").
You may now wonder... what if I need a backslash character in my string? Just double it in that case. In your code for example just use '\\'.
Here is a table for the possible meanings of backslash in strings
\" just a regular double-quote character, it doesn't end the string
\' just a regular single-quote character, it doesn't end the string
\b a backspace character (ASCII code 0x08)
\t a tab character (ASCII code 0x09)
\n a newline character (ASCII code 0x0A)
\v a vertical tab character (ASCII code 0x0B)
\f a form feed character (ASCII code 0x0C)
\r a carriage return character (ASCII code 0x0D)
\033 the character with ASCII code 033 octal = 27 ("ESC" in this case)
\x41 the character with ASCII code 0x41 = 65 ("A" in this case)
\u05D0 the unicode character 0x05D0 (Aleph from the Hebrew charset)
\\ just regular backslash character, not an escape prefix

\ is an escape character. You'll have to double it to literally mean a backslash character, otherwise it'll augment the following character (In this case the next single quote)

You need to properly escape the backslash:
var lastMenstrualPeriod = '\\' + obj[0]['LastMenstrualPeriod'] + '/';
Being escape character, the JS "compiler" is expecting another character to follow, for example \n is newline constant, \t is tab etc.. so \\ is one single backslash in a string.
It is also mentioned in Douglas Crockford book.

You are forgetting to escape '\'
Do this:
var lastMenstrualPeriod = '\\' + obj[0]['LastMenstrualPeriod'] + '/';

Related

Javascript substring from last backslash char

This is probably a basic syntax question, but I am not able to find a syntax for a backslash character. Following and other syntaxes that I tried are not accepted for this char.
var fileNameSubstring = data.FileName.substring(data.FileName.lastIndexOf('\') + 1, data.FileName.length);
When defining a string in Javascript you can use the backslash (called escape character) to indicate special characters like new line \n.
To actually have a backslash in your string you should use double blackslash \\.
var fileNameSubstring = data.FileName.substring(data.FileName.lastIndexOf('\\') + 1, data.FileName.length);

Can Someone explain escaping a string in JavaScript

I know this line escapes a javascript string by adding \ before special characters. But can anyone explain how it does that? And also. Can it cause any problems in further string manipulations.
str = str.replace(/[-\\/\\^$*+?.()|[\]{}]/g, '\\$&');
The regular expression replaces anything it matches with backslash and the matched substring. It matches any single characters of the set between [ and ]. That is:
- simple dash
\\ single backslash (preceded by another backslash, because it server as escape character)
/ slash
\\ single backslash again (it is obsolete here)
^, $, *, +, ?, ., (, ), |, [ single characters
\] closing bracket is preceded by escape character, otherwise it would close set of characters to match
{, } single characters
The replacement string \\$& simply says:
\\ add single backslash
$& add the whole substring matched by the expression

Regexp adds invisible dot character replacing \b

I want this to be my regex: /^word\b/ (word is dynamic)
When I set it up to be dynamic I have to use this:
var word='spoon';
'spoon .table .chair'.match(new RegExp('^'+word+'\b'));
However, this finds null, while this:
var word='spoon';
'spoon .table .chair'.match(/^spoon\b/);
finds ["spoon"].
The interesting part is when I examine the difference between the regex I worte and the regex RegExp wrote:
console.log(/^spoon\b/,new RegExp('^'+word+'\b'))
It shows this:
/^spoon\b/ /^spoon/
If I then copy the second part of the log output (/^spoon/) into my code editor I see this character:
What is that? How do I do RegExp word-ending-with as I am not always guaranteed to have a space at the end when the string might be a one-word string (spoon or another word)
I'd rather just do this without the invisible thing
You've got to escape the \ in the b in the regex string by adding an extra slash:
var regex = new RegExp('^' + word + '\\b')
This is because the RegExp is expecting to see the two characters \ and b, but the string '\b' is one character, ascii 8, the backspace character (in the same way that '\n' is a single newline character).
In Javascript, \b doesn't mean a \ followed by a b. It means the backspace character (ASCII code 8). To get a \ followed by a b, you need to escape the slash so that Javascript doesn't parse it as a backspace:
'^' + word + '\\b'
The same thing applies if you want to use \d or \s or anything else: You need to escape the \ with another one so that Javascript doesn't think it's a Javascript escape code and the RegExp can parse it as what you expect.

How can I replace multiple slashes in javascript?

var str = "Hello\\\World\\\";
var newStr = str.replace("\\\", "");
alert(newStr); // I want this to alert: HelloWorld
The number of slashes is always 3, not more not less. How can I replace them? The code above doesn't work at all. I've played around a bit with the global flag, escaping the slashes etc but can't figure it out.
Firstly, you need to escape each slash with another backslash, as mentioned by #Bathsheba.
Additionally, you want your replacement regex to be global:
var str = "Hello\\\\\\World\\\\\\";
var newStr = str.replace(/\\\\\\/g, "");
alert(newStr); // I want this to alert: HelloWorld
If you want three slashes in a row in a string literal then you need to escape each one in turn:
var str = "Hello\\\\\\World\\\\\\";
var newStr = str.replace("\\\\\\", "");
In your current string, \\\W would be one slash and an error as \W is not a valid sequence. (Some more examples: \\ is a single slash, \t a tab, \" a quotation character).
Try this regex \\\\\\ for replace
\\ indicates \
There are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters".
If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash.

Replace '\' with '-' in a string

I have seen all the questions asked previously but it didn't help me out . I have a string that contains backslash and i want to replace the backslashes with '-'
var s="adbc\sjhf\fkjfh\af";
s = s.replace(/\\/g,'-');
alert(s);
I thought this is the proper way to do it and of course i am wrong because in alert it shows adbcsjhffkjfhaf but i need it to be like adbc-sjhf-fkjfh-af.
What mistake i do here and what is the reason for it and how to achieve this...??
Working JS Fiddle
Your s is initially adbcsjhffkjfhaf. You meant
var s="adbc\\sjhf\\fkjfh\\af";
You need to double-up the backslashes in your input string:
var s="adbc\\sjhf\\fkjfh\\af";
Prefixing a character with '\' in a string literal gives special meaning to that character (eg '\t' means a tab character). If you want to actually include a '\' in your string you must escape it with a second backslash: '\\'
Javascript is ignoring the \ in \s \f \a in your string. Do a console.log(s) after assigning, you will understand.
You need to escape \ with \\. Like: "adbc\\sjhf\\fkjfh\\af"
The string doesn't contain a backslash, it contains the \a, \s and \f (escape sequence for Form Feed).
if you change your string to adbc\\sjhf\\fkjfh\\af
var s="adbc\\sjhf\\fkjfh\\af";
s = s.replace(/\\/g,'-');
alert(s);
you will be able to replace it with -

Categories

Resources