Difference (if there is any) between ` and ' in javascript [duplicate] - javascript

This question already has answers here:
Usage of the backtick character (`) in JavaScript
(11 answers)
Closed 6 years ago.
Recently ran into some JS code that uses ` and '. I can't figure out if there is a different use for each apostrophe. Is there any?

' or " denotes a string
` denotes a template string. Template strings have some abilities that normal strings do not. Most importantly, you get interpolation:
var value = 123;
console.log('test ${value}') //=> test ${value}
console.log(`test ${value}`) //=> test 123
And multiline strings:
console.log('test
test')
// Syntax error
console.log(`test
test`)
// test
// test
They have some other tricks too, more on template strings here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
Note they are not supported in all javascript engines at the moment. Which is why template strings are often used with a transpiler like Babel. which converts the code to something that will work in any JS interpreter.

Related

template strings not working [duplicate]

This question already has answers here:
Template literals like 'some ${string}' or "some ${string}" are not working
(7 answers)
Closed 3 years ago.
Template Strings should work on any terminals such as visual studio code terminal or windows terminal. But it didn't. I did this code visual studio code. Here is My code
var name = 'Andrew';
console.log('Hello ${name}');
and the output is
Hello ${name}
Please specify changes in my code needed and explain why it doesn't work currently.
Single and double quotes wont invoke the behavior - use back ticks.
var name = 'Andrew';
console.log(`Hello ${name}`);
// ^ ^
More information on Template literals,
All that is inside a string, is literal. You're writing the variable ${name} inside the normal quotes, so it will be printed literal.
If you want to have it interpretated, you have to concatenate the answer, as for example:
console.log('Hello ' + name)
The quotes to use a template are not the ones you are using, these are the correct ones: (closed accents / back-tick )
`Hello ${name}`
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
var name = 'Andrew';
console.log(`Hello ${name}`);
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. They were called "template strings" in prior editions of the ES2015 specification.
Template literals are enclosed by the back-tick (``) (grave accent) character instead of double or single or double quotes.
It's not a quote, nor double quote
var name = 'Andrew'
console.log(`Hello ${name}`)
Here is a tutorial about it: https://babeljs.io/learn-es2015/#template-strings

Regex capture everything between two tags across multiple lines [duplicate]

This question already has answers here:
JavaScript regex multiline text between two tags
(5 answers)
Closed 7 years ago.
I have this regex in Ruby: http://rubular.com/r/eu9LOQxfTj
/<sometag>(.*?)<\/sometag>/im
And it successfully matches input like this:
<sometag>
123
456
</sometag>
Which would return
123
456
However, when I try this in javascript (testing in chrome), it doesn't match anything.
Does javascript's multiline flag mean something else?
I want to capture everything non-greedily between two given tags.
How can I accomplish this in javascript using regex? Here is a Debuggex Demo
<sometag>(.*?)<\/sometag>
This is not XML parsing.
Javascript does not support multiline expressions when using . alone. You have to use [\s\S] in place of . so an example that satisfies what you want would be:
var x = "<sometag>\n\
123\n\
456\n\
</sometag>";
var ans = x.match(/<sometag>([\s\S]*?)<\/sometag>/im).pop();
// ans equals " 123 456"
note that you still need the m modifier.

Escaping text for regex [duplicate]

This question already has answers here:
Javascript equivalent of Perl's \Q ... \E or quotemeta()
(3 answers)
Closed 2 years ago.
In Perl, there's a function named quotemeta which accepts a string and returns a regex pattern that matches that string. It's used in virtually every program to avoid code injection bugs.
One would use quotemeta when dynamically building a pattern. For example,
"^"+quotemeta(var)+"_\\d+$"
A JavaScript implementation follows:
function quotemeta(s) {
return String(s).replace(/\W/g, "\\$&");
}
Given how needed this function when working with regex patterns, I would have expected JavaScript to provide one. Does JavaScript or jQuery already have such a function?
JavaScript doesn't have such a method natively. (And jQuery doesn't include one)
Usually, when searching for a string pattenr, you'd use String.prototype.indexOf. This method find a string in a string, so you won't even need to convert the string pattern to a regex.
String.prototype.replace can also take a String pattern.
It is not exactly the same but it'll work for most string matching use cases.

Difference between " and ' [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
When to Use Double or Single Quotes in JavaScript
single quotes versus double quotes in js
I'm trying to build a Node.js Express web application, and in the tutorial they use ' instead of " quite often, but there is no explanation why.
Can someone please explain the difference? Is this specific to JavaScript, or does it apply to other languages too?
Example:
app.configure('dev')
app.get("/", function (req, res)
Thanks :)
In JavaScript, both are equivalent. The only difference is that inside a single-quoted string you don't have to escape ", and vice versa:
'dev' === "dev"
'd"v' === "d\"v"
'd\'v' === "d'v"
Most other languages distinguish the two in some way. For example, in Bash and Perl, '' prevents variables from being expanded inside, so 'a$b' is the actual string a$b, whereas "a$b" is the string consisting of a plus the value of the variable b. In C, C++, C#, and Java, '' is used to create a single character constant, so that 'a' means the character a whereas "a" means a string containing that character.
Javascript string literals can be enclosed with ' or "; there is no difference between them (except for nesting).
This is not true in most other languages.

String.prototype.replaceAll() not working [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 2 years ago.
I need to replace all the string in a variable.
var a = "::::::";
a = a.replace(":", "hi");
console.log(a);
The above code replaces only the first string i.e..hi::::::
I used replaceAll but it's not working.
Update: All recent versions of major browsers, as well as NodeJS 15+ now support replaceAll
Original:
There is no replaceAll in JavaScript: the error console was probably reporting an error.
Instead, use the /g ("match globally") modifier with a regular expression argument to replace:
const a = "::::::";
const replaced = a.replace(/:/g,"hi");
console.log(replaced);
The is covered in MDN: String.replace (and elsewhere).
There is no replaceAll function in JavaScript.
You can use a regex with a global identifier as shown in pst's answer:
a.replace(/:/g,"hi");
An alternative which some people prefer as it eliminates the need for regular expressions is to use JavaScript's split and join functions like so:
a.split(":").join("hi");
It is worth noting the second approach is however slower.

Categories

Resources