使用 JavaScript 在字串中獲取撇號

Mehvish Ashiq 2023年10月12日
  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
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