Dấu ngoặc đơn thoát JavaScript
Khi làm việc với chuỗi, bạn sẽ nhận thấy có một số ký tự dường như luôn phá vỡ chương trình của bạn. Chúng bao gồm dấu nháy đơn, dấu và, dấu ngoặc kép, v.v. Show Khi làm việc với các ký tự này, bạn cần sử dụng ký tự được gọi là ký tự thoát. Một ký tự thoát cho phép bạn xuất các ký tự mà bình thường bạn không thể làm được, thường là do trình duyệt sẽ diễn giải ký tự đó khác với những gì bạn dự định Trong JavaScript, dấu gạch chéo ngược (\) là một ký tự thoát Ví dụ: giả sử tôi muốn hiển thị văn bản sau. Họ gọi đó là nhân vật "thoát" Hãy thử điều đó mà không có ký tự thoát Không có nhân vật trốn thoátĐoạn mã trên sẽ không hoạt động như dự định vì ngay khi trình duyệt gặp dấu ngoặc kép đầu tiên, nó sẽ nghĩ rằng chuỗi đã kết thúc. Hơn nữa, nó sẽ dẫn đến lỗi vì nó sẽ mong đợi dấu ngoặc đóng Mã có ký tự thoátĐoạn mã trên sẽ hiển thị dấu ngoặc kép như dự định Điều này là do, ngay khi trình duyệt gặp dấu gạch chéo ngược, nó sẽ biết không cố diễn giải ký tự tiếp theo Ký tự Escape là biểu tượng được sử dụng để bắt đầu lệnh thoát nhằm thực hiện một số thao tác. Chúng là những ký tự có thể được giải thích theo một số cách thay thế khác với những gì chúng tôi dự định. Javascript sử dụng '\' (dấu gạch chéo ngược) ở phía trước làm ký tự thoát Mục đích của chúng tôi là chúng tôi muốn in trong bảng điều khiển như ""Geeks" for "Geeks" is 'the' best 'platform'" Để in dấu ngoặc kép, sử dụng các ký tự thoát, chúng tôi có hai tùy chọn
Chúng tôi cũng có thể in dấu ngoặc kép trong bảng điều khiển bằng cách sử dụng dấu ngoặc đơn và dấu ngoặc kép mà không cần sử dụng ký tự thoát. Nhưng có một hạn chế là chúng ta chỉ có thể in dấu ngoặc đơn hoặc dấu ngoặc kép. Nếu chuỗi được biểu diễn bằng dấu ngoặc đơn thì chúng ta chỉ có thể in dấu ngoặc kép và nếu chuỗi được biểu thị bằng dấu ngoặc đơn thì chúng ta có thể in dấu ngoặc kép bên trong nó. Các chuỗi được biểu diễn trong dấu nháy đơn hoặc kép là như nhau, không có sự khác biệt Javascripttrue Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks6 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks7 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks9 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks1 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks2 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks0 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks7 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks3 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks5 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks6 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks0 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks7 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 Geeks 'FOR' Geeks Geeks "FOR" Geeks 'Geeks "FOR" Geeks' ""Geeks" for "Geeks" is 'the' best 'platform'"0 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 Geeks 'FOR' Geeks Geeks "FOR" Geeks 'Geeks "FOR" Geeks' ""Geeks" for "Geeks" is 'the' best 'platform'"2 Geeks 'FOR' Geeks Geeks "FOR" Geeks 'Geeks "FOR" Geeks' ""Geeks" for "Geeks" is 'the' best 'platform'"3 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 Geeks 'FOR' Geeks Geeks "FOR" Geeks 'Geeks "FOR" Geeks' ""Geeks" for "Geeks" is 'the' best 'platform'"5 Geeks 'FOR' Geeks Geeks "FOR" Geeks 'Geeks "FOR" Geeks' ""Geeks" for "Geeks" is 'the' best 'platform'"6 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 Geeks 'FOR' Geeks Geeks "FOR" Geeks 'Geeks "FOR" Geeks' ""Geeks" for "Geeks" is 'the' best 'platform'"8 Geeks 'FOR' Geeks Geeks "FOR" Geeks 'Geeks "FOR" Geeks' ""Geeks" for "Geeks" is 'the' best 'platform'"6 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks7 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks62 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks64 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks66 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks68 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks69 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks0 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks72____173 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks7 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks76 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks78 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks80 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks81 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks0 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks8 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks72____185 true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks86 đầu ra true Geeks for Geeks Geeks for Geeks "Geeks" "FOR" Geeks 'Geeks' 'FOR' Geeks ví dụ 2. Sử dụng chuỗi thoát – Nếu bạn đã bắt đầu trích dẫn bằng \’ thì bạn cũng phải kết thúc trích dẫn bằng \’ và ngược lại Làm cách nào để thoát khỏi dấu ngoặc đơn trong JavaScript?Để sử dụng ký tự đặc biệt làm ký tự thông thường, hãy thêm dấu gạch chéo ngược vào trước ký tự đó. \ . . Đó cũng được gọi là "thoát khỏi một nhân vật".
Làm cách nào để tránh các ký tự thoát trong JavaScript?Bạn có thể thực hiện thay thế đơn giản. str. replace("\t","\\t");
Làm cách nào để bỏ qua các ký tự đặc biệt trong JavaScript?Sử dụng phương thức thay thế() để xóa tất cả ký tự đặc biệt khỏi chuỗi , e. g. str. thay thế(/[^a-zA-Z0-9 ]/g, '');. Phương thức thay thế () sẽ trả về một chuỗi mới không chứa bất kỳ ký tự đặc biệt nào.
Những ký tự nào cần được thoát khỏi JavaScript?Trong HTML, chúng tôi sử dụng dấu ngoặc kép và trong dấu ngoặc đơn JavaScript, vì vậy bất kỳ dấu ngoặc kép nào trong mã JavaScript sẽ cần phải được . |