欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

怎樣使用?JavaScript?轉(zhuǎn)義字符串中的引號(hào)

 更新時(shí)間:2023年11月25日 11:34:07   作者:火焰兔  
要轉(zhuǎn)義字符串中的單引號(hào)或雙引號(hào),需要在字符串內(nèi)容中的每個(gè)單引號(hào)或雙引號(hào)之前使用反斜杠?\?字符,例如?‘that’s?it’,這篇文章主要介紹了如何使用?JavaScript?轉(zhuǎn)義字符串中的引號(hào),需要的朋友可以參考下

在 JavaScript 中轉(zhuǎn)義字符串中的引號(hào)

要轉(zhuǎn)義字符串中的單引號(hào)或雙引號(hào),需要在字符串內(nèi)容中的每個(gè)單引號(hào)或雙引號(hào)之前使用反斜杠 \ 字符,例如 ‘that’s it’。

const escapeSingle = 'it\'s a string';
console.log(escapeSingle) // ??? it's a string

const escapeDouble = "fql\"zadmei"
console.log(escapeDouble)  // ??? fql"zadmei

當(dāng)我們使用反斜杠字符轉(zhuǎn)義單引號(hào)或雙引號(hào)時(shí),我們指示 JavaScript 我們希望將引號(hào)視為文字單引號(hào)或雙引號(hào)字符,而不是字符串結(jié)尾字符。 如果在用單引號(hào)引起來的字符串中使用單引號(hào),則會(huì)過早地終止該字符串。

但是,當(dāng)我們使用反斜杠轉(zhuǎn)義單引號(hào)時(shí),情況就不是這樣了。

轉(zhuǎn)義字符串中的雙引號(hào)

我們可以使用相同的方法來轉(zhuǎn)義字符串中的雙引號(hào)。

const escapeDouble = "He said: \"test 123\""
console.log(escapeDouble) // ??? He said: "test 123"

我們使用反斜杠 \ 字符來轉(zhuǎn)義字符串中的每個(gè)雙引號(hào)。

使用 String.replaceAll() 轉(zhuǎn)義字符串中的引號(hào)

我們還可以使用 String.replaceAll() 方法轉(zhuǎn)義字符串中的引號(hào)。

// ? 轉(zhuǎn)義每一個(gè)單引號(hào)
const str = "it's a string";
console.log(str); // ??? it's a string
const result = str.replaceAll("'", "\\'");
console.log(result); // ??? it\'s a string

String.replaceAll() 方法返回一個(gè)新字符串,其中所有匹配的模式都被提供的替換項(xiàng)替換。

該方法采用以下參數(shù):

  • pattern 要在字符串中查找的模式。 可以是字符串或正則表達(dá)式。
  • replacement 一個(gè)字符串,用于將匹配的子字符串替換為提供的模式。

代碼示例對(duì)字符串中的每個(gè)單引號(hào)進(jìn)行轉(zhuǎn)義。

// ? 轉(zhuǎn)義每一個(gè)單引號(hào)
const str = "it's a string";
console.log(str); // ??? it's a string
const result = str.replaceAll("'", "\\'");
console.log(result); // ??? it\'s a string

可以使用相同的方法來轉(zhuǎn)義字符串中的雙引號(hào)。

// ? 轉(zhuǎn)義每一個(gè)雙引號(hào)
const str = 'it"s a string';
console.log(str); // ??? it"s a string
const result = str.replaceAll('"', '\\"');
console.log(result); // ??? it\"s a string

String.replaceAll() 方法返回一個(gè)新字符串,其中替換了模式的匹配項(xiàng)。 該方法不會(huì)更改原始字符串。

字符串在 JavaScript 中是不可變的。

交替引號(hào)以避免使用反斜杠

可以通過更改字符串的外部引號(hào)來避免轉(zhuǎn)義引號(hào)。

const withSingle = "it's a string";
console.log(withSingle) // ??? it's a string
const withDouble = 'He said: "test 123"'
console.log(withDouble) // ??? He said: "test 123"

我們交替使用雙引號(hào)和單引號(hào),因此我們不必轉(zhuǎn)義它們。

使用反引號(hào)代替反斜杠

請(qǐng)注意 ,我們還可以在定義存儲(chǔ)字符串的變量時(shí)使用反引號(hào)。 這允許我們?cè)谧址型瑫r(shí)使用單引號(hào)和雙引號(hào)而無需轉(zhuǎn)義它們。

const withBoth = `it's a "test 123"`;
console.log(withBoth) // ??? it's a "test 123"

該字符串用反引號(hào)括起來,因此我們不必在其內(nèi)容中轉(zhuǎn)義單引號(hào)和雙引號(hào)。

要將反斜杠 \ 字符添加到字符串,請(qǐng)將兩個(gè)反斜杠并排添加。

第一個(gè)反斜杠轉(zhuǎn)義第二個(gè)反斜杠,因此第二個(gè)反斜杠按字面意思。

const addBackslash = "He said: \\\"test 123\\\""
console.log(addBackslash) // ??? He said: \"test 123\"

我們有 3 個(gè)相鄰的反斜杠。 第一個(gè)反斜杠轉(zhuǎn)義第二個(gè)反斜杠,因此它由 JavaScript 按字面解釋。 第三個(gè)反斜杠用于轉(zhuǎn)義雙引號(hào)。

這是一個(gè)更現(xiàn)實(shí)的例子,我們只在字符串中添加一個(gè)反斜杠。

const addBackslash = "BMW \\1996\\"
console.log(addBackslash) // ??? BMW \1996\

顯示字符串中的轉(zhuǎn)義字符

如果需要在字符串中顯示轉(zhuǎn)義字符,請(qǐng)使用 JSON.stringify() 方法。

const addBackslash = 'BMW \\1996\\';
console.log(addBackslash); // ??? BMW \1996\
const withEscapeChars = JSON.stringify(addBackslash);
console.log(withEscapeChars); // ??? "BMW \\1996\\"

JSON.stringify 方法將 JavaScript 值轉(zhuǎn)換為 JSON 字符串。

將值轉(zhuǎn)換為 JSON 字符串時(shí),其輸出包含轉(zhuǎn)義字符。

避免在 HTML 代碼中使用內(nèi)聯(lián)事件處理程序

如果在 HTML 代碼中使用內(nèi)聯(lián)事件處理程序時(shí)出現(xiàn)錯(cuò)誤,最好的解決方案是重寫代碼以使用 JavaScript 而不是使用內(nèi)聯(lián)事件處理程序。

這是一個(gè)例子。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
  </head>
  <body>
    <div id="box">zadmei.com</div>
    <script type="module" src="index.js"></script>
  </body>
</html>

這是相關(guān)的 JavaScript 代碼。

const box = document.getElementById('box');
function handleClick() {
  box.style.background = 'lime';
}
box.addEventListener('click', handleClick);
box.innerHTML = `
  <div id="box">
    <p title="example">zadmei.com</p>
    <p>Hello world</p>
  </div>
`;

我們可以使用 document.getElementById() 方法或 querySelector 方法選擇 DOM 元素。

然后,我們可以向元素添加事件偵聽器或更新其內(nèi)部 HTML 標(biāo)記。

請(qǐng)注意 ,我們?cè)谠O(shè)置 innerHTML 屬性時(shí)使用了反引號(hào)。

這使我們能夠在構(gòu)建 HTML 標(biāo)記時(shí)同時(shí)使用單引號(hào)和雙引號(hào)。

到此這篇關(guān)于如何使用 JavaScript 轉(zhuǎn)義字符串中的引號(hào)的文章就介紹到這了,更多相關(guān)js轉(zhuǎn)義字符串內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論