在 JavaScript 的 ES6 版本中,引入了文字。 JavaScript 包含对象文字、数组文字、数字文字、RegExp 文字等。此外,它还包含字符串文字。
字符串文字允许我们创建不带任何反斜杠字符的多行字符串,在引号中添加任何单词或句子,以及在字符串之间添加变量和数学表达式。
语法
用户可以按照以下语法在 ECMAScript 6 中使用模板字符串文字。
let string = `This is template literal string!`;
在上面的语法中,我们使用反引号 (``) 来创建模板文字字符串。
示例 1(多行字符串)
在下面的示例中,我们使用模板文字字符串来创建多行字符串。每当我们创建带引号的字符串时,我们都需要使用“
”字符来创建新行,但是使用字符串文字,我们可以通过在新行中写入字符串来实现。
在 string1 中,新行是通过在新行中写入字符串来创建的,而在 string2 中,我们使用“
”字符来创建新行。用户可以观察输出中的 string1 和 string2,它们看起来是相同的。
let string1 = `This is first line. This is the second line. This is the third line. This is the fourth line.`; console.log(string1); // added character to create a multiline string. let string2 = "Welcome on the TutorialsPoint!"; console.log(string2);
示例 2(字符串中的引号)
我们可以使用模板字符串文字在字符串内添加引号。当我们创建带有双引号的字符串时,我们只能为该字符串添加单引号,而当我们创建带有单引号的字符串时,我们也只能为该字符串添加双引号。
我们使用字符串文字在 stringQuote 变量的字符串中添加了单引号。
<html> <body> <h2 id="Using-the-i-template-string-literals-i-to-add-quotes-in-the-string">Using the <i>template string literals</i> to add quotes in the string.</h2> <div id = "output"></div> </body> <script> var output = document.getElementById('output'); let stringQuotes = `This is a 'template string literals' with a quote.`; output.innerHTML += stringQuotes + "<br/>"; let string1 = "This is 'similar to template string literals'." + "<br/>"; output.innerHTML += string1; </script> </html>
示例 3(字符串中的变量)
在下面的示例中,我们在字符串中完成了变量替换。一般来说,要在字符串中使用变量,我们需要使用“+”运算符并连接多个字符串,但模板字符串文字允许我们直接在字符串中添加变量。我们可以在 ${} 表达式中添加变量。
在variableStr变量的值中,我们插入了name、job和timePeriod变量。
<html> <body> <h2 id="Using-the-i-template-string-literals-i-to-add-variables-in-the-string">Using the <i>template string literals </i> to add variables in the string.</h2> <div id = "output"> </div> </body> <script> var output = document.getElementById('output'); let name = "Shubham"; let job = "Content writer"; let timePeriod = "1 Year"; let variableStr = `Using template string literals :- ${name} is a ${job} at TutorialsPoint from last ${timePeriod}.`; output.innerHTML += variableStr + "<br/>"; let string = "Using Quotes :- " + name + " is a " + job + " at TutorialsPoint from last " + timePeriod + ". "; output.innerHTML += string + "<br/>"; </script> </html>
示例 4(字符串中的表达式)
在此示例中,我们将使用模板字符串文字在字符串中添加数学表达式。在 sumString 中,我们在 ${} 内添加了数学表达式。用户可以看到我们如何在字符串中对 num1 和 num2 求和。
此外,我们还对 string2 中的 2 个值进行了乘法运算。
<html> <body> <h2 id="Using-the-i-template-string-literals-i-to-add-expression-in-the-string">Using the <i> template string literals </i> to add expression in the string.</h2> <div id = "output"> </div> </body> <script> var output = document.getElementById('output'); let num1 = 10; let num2 = 40; let sumString = `The sum of ${num1} and ${num2} is ${num1 + num2}`; output.innerHTML += sumString + "<br>"; let string2 = `The multiplication of 20 and 5 is ${20 * 5}`; output.innerHTML += string2 + "<br>"; </script> </html>
示例 5(字符串中的 HTML)
我们可以使用模板字符串文字创建一行 HTML 并将其添加到网页中。在此示例中,我们使用字符串文字创建了 HTML 列表,并使用 的innerHTML 属性在网页中添加行 HTML。
<html> <body> <h2 id="Using-the-i-template-string-literals-i-to-add-HTML-to-the-document">Using the <i>template string literals</i> to add HTML to the document.</h2> <div id = "output"> </div> </body> <script> var output = document.getElementById('output'); let HTMLString = `<ul> <li> One </li> <li> Two </li> <li> Three </li> <li> Four </li> <li> Five </li> </ul>`; output.innerHTML = HTMLString; </script> </html>
用户学会了在 JavaScript 中使用模板字符串文字。我们已经了解了如何创建多行字符串、变量和表达式替换、添加引号以及使用模板字符串文字创建行 HTML。
以上是如何在 ECMAScript 6 中使用模板字符串文字?的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaScript字符串替换方法详解及常见问题解答 本文将探讨两种在JavaScript中替换字符串字符的方法:在JavaScript代码内部替换和在网页HTML内部替换。 在JavaScript代码内部替换字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 该方法仅替换第一个匹配项。要替换所有匹配项,需使用正则表达式并添加全局标志g: str = str.replace(/fi

简单JavaScript函数用于检查日期是否有效。 function isValidDate(s) { var bits = s.split('/'); var d = new Date(bits[2] '/' bits[1] '/' bits[0]); return !!(d && (d.getMonth() 1) == bits[1] && d.getDate() == Number(bits[0])); } //测试 var

本文探讨如何使用 jQuery 获取和设置 DOM 元素的内边距和外边距值,特别是元素外边距和内边距的具体位置。虽然可以使用 CSS 设置元素的内边距和外边距,但获取准确的值可能会比较棘手。 // 设置 $("div.header").css("margin","10px"); $("div.header").css("padding","10px"); 你可能会认为这段代码很

本文探讨了十个特殊的jQuery选项卡和手风琴。 选项卡和手风琴之间的关键区别在于其内容面板的显示和隐藏方式。让我们深入研究这十个示例。 相关文章:10个jQuery选项卡插件

发现十个杰出的jQuery插件,以提升您的网站的活力和视觉吸引力!这个精选的收藏品提供了不同的功能,从图像动画到交互式画廊。让我们探索这些强大的工具: 相关文章: 1

HTTP-Console是一个节点模块,可为您提供用于执行HTTP命令的命令行接口。不管您是否针对Web服务器,Web Serv

本教程向您展示了如何将自定义的Google搜索API集成到您的博客或网站中,提供了比标准WordPress主题搜索功能更精致的搜索体验。 令人惊讶的是简单!您将能够将搜索限制为Y

当div内容超出容器元素区域时,以下jQuery代码片段可用于添加滚动条。 (无演示,请直接复制到Firebug中) //D = document //W = window //$ = jQuery var contentArea = $(this), wintop = contentArea.scrollTop(), docheight = $(D).height(), winheight = $(W).height(), divheight = $('#c


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver Mac版
视觉化网页开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。