What is the difference between single and double quotes? [duplicate] - javascript

This question already has answers here:
Are double and single quotes interchangeable in JavaScript?
(23 answers)
Closed 8 years ago.
What is difference between 'string' and "string"?
For example, I use
alert('abc') and alert("abc")
document.getElementById('id') and document.getElementById("id")
I don't see any difference.

There is no difference between them in javascript.
It is used to escape from escaping the quotes by using the alternative.
Like:
'some string "some here"';
"some string ' some here'";
You don't need to escape them using \ as in most language.

There is no difference in JavaScript. Other languages handle single and double quotes differently. For example, with PHP, you can use double quotes to perform variable substitution.
One thing that might be relevant is the JSON (JavaScript Object Notation) spec in which all strings should be wrapped with double quotes. Most languages will be able to handle both types of quotes in the same manner, but if you need to have a 100% valid JSON object then you'll need to use double quotes.

Related

invalid Regex group [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to create the following regex using Javascript.
(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)
However, by doing this it gives me invalid group error in the console.
regExp = new RegExp("(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)", "gi");
I don't understand where the problem comes from exactly. I appreciate the help.
Thank you
EDIT: After some research I found that Javascript does not support lookbehinds.
So the error comes from (?<!\\).
Refer this newly asked question to find an alternative way to do the same job.
How to check for odd numbers of backslashes in a regex using Javascript?
If your expression isn't dynamic, just use a literal:
var regExp = /(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)/gi;
The problem is that your escape sequences \\ inside the string end up rendering \ characters inside the regEx, which in turn end up escaping brackets they shouldn't, resulting in unterminated groups.

Backslash Discrepancy between Regex Constructor and Literals [duplicate]

This question already has an answer here:
javascript why double escape dot/character [closed]
(1 answer)
Closed 7 years ago.
The title sums it up. I came across an odd discrepancy in backslash escaping between regular expression literals and constructor functions with new RegExp(), and I was curious about what's behind it.
I was trying to escape a parenthesis ( inside a constructor, like so:
var search = new RegExp('/(', 'g');
var result = "(test)".match(search);
But this kept returning an error. The match worked fine inside a literal /\(/g;, but inside the constructor I ended up having to do something like this:
search = new RegExp('\\(', 'g');
Can someone please explain to me why an escaping backslash requires an escaping backslash itself in a constructor, but not a literal?
Because the backslash is a special character in both the context of a regexp, and the context of a string literal. You have to get past the string literal's special usage before the regexp parser can see it and apply its own special rules.
NOTE If pattern is a StringLiteral, the usual escape sequence substitutions are performed before the String is processed by RegExp. If pattern must contain an escape sequence to be recognised by RegExp, any backslash \ characters must be escaped within the StringLiteral to prevent them being removed when the contents of the StringLiteral are formed.
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.4.1

Javascript regex to extract the string before the last backslash [duplicate]

This question already has answers here:
Regex for everything before last forward or backward slash
(3 answers)
Closed 9 years ago.
I am dealing with timezone's in Javascript and I need a regex that will extract everything, but the timezone name from it. For example, I have the timezone America/Argentina/Buenos_Aires. I want to extract the America/Argentina part with a regex. Currently I have this regex: tz.match(/.*?(?=\/|$)/i)[0] which extracts everything to the first backslash which works for most timezones (America/Los_Angeles), but not for all of them. How could I edit that regex so that it gets the string before the last value?
I'd personally suggest avoiding regular expressions for something like this, when simple string functions/methods would suffice admirably:
var stringVariable = 'America/Argentina/Buenos_Aires',
text = stringVariable.substring(0, stringVariable.lastIndexOf('/'));

How can I use regex to replace all $#8211; with – in javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
replace all occurrences in a string
I found this question/answer:
Use JavaScript regex to replace numerical HTML entities with their actual characters
I just need to replace the one entity though. How can I match that specific pattern with a regex?
I don't know much about regex so I've done this:
.replace('–', '–')
But it obviously only replaces the first instance.
Thanks,
Thomas
The replace method only replaces the first occurance when you are using a string. Use a regular expression, so that you can specify the global flag g:
.replace(/–/g, '–')
.replace(/–/g, '–')
the g flag means global so it replaces all instances.

Double quotes vs single quotes in JavaScript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
When to Use Double or Single Quotes in JavaScript
Are there differences between ' and "
I am wondering if there is a difference between using single quotes vs double quotes in JavaScript (JQuery selectors etc.).
Both seem to work just fine, so is there a difference?
The difference is that you don't need to escape single quotes in double quotes, or double quotes in single quotes. That is the only difference, if you do not count the fact that you must hold the Shift key to type ".
It doesn't matter unless you're trying to write JSON, in which case you must use double quotes for object literals.
There is no special difference between single and double quotes (like it is in PHP).
The only actual difference is that you can write a double quote " in a single-quoted string without escaping it, and vice versa.
So, if you are going to output some HTML in your JavaScript, like this:
<div id = "my_div"></div>
single quotes are more useful :-)
They work the same, but they must match. You cannot start a string with a single and end with a double, or the opposite. Other than that, they are interchangeable.

Categories

Resources