使用 JavaScript 在字符串中获取撇号

Mehvish Ashiq 2023年1月30日 2022年5月5日
  1. 在 JavaScript 中使用双引号 ("") 来得到字符串中的撇号
  2. 在 JavaScript 中使用转义字符 (\) 获取字符串中的撇号
使用 JavaScript 在字符串中获取撇号

本教程解释了如何使用 JavaScript 通过用单引号和双引号将字符串括起来来获取字符串中的撇号。

在 JavaScript 中使用双引号 ("") 来得到字符串中的撇号

var message = "Hi, Let's start reading if you are free!";
console.log(message);

输出:

"Hi, Let's start reading if you are free!"

假设字符串在字符串中的多个位置有撇号; 我们还能用双引号吗?可以,请看下面的例子。

var message = "I can start reading soon when I'll be free from this project, but it doesn't seem to happen.";
console.log(message);

输出:

"I can start reading soon when I'll be free from this project, but it doesn't seem to happen."

在 JavaScript 中使用转义字符 (\) 获取字符串中的撇号

var message = "Hi, Let\'s start reading if you are free!";
console.log(message);

输出:

"Hi, Let's start reading if you are free!"

你可能会想,如果我们使用转义字符,我们可以将字符串用单引号括起来而不影响结果吗?是的当然。

var message = 'Hi, Let\'s start reading if you are free!';
console.log(message);

输出:

"Hi, Let's start reading if you are free!"

我们可以在字符串中任何需要的地方转义撇号。

var message = 'I\'ll start reading soon when I\'ll be free from this project, but it doesn\'t seems to happen.';
console.log(message);

输出:

"I'll start reading soon when I'll be free from this project, but it doesn't seem to happen."
Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

相关文章 - JavaScript String