Hướng dẫn convert special characters to string javascript - chuyển ký tự đặc biệt thành chuỗi javascript
Bài viết này thảo luận về việc thay thế tất cả các ký tự đặc biệt trong chuỗi JavaScript bằng các phương thức và ví dụ khác nhau. Mục lục:- JavaScript & nbsp; thay thế () & nbsp; phương thức thay thế một số hoặc tất cả các lần xuất hiện của một mẫu bằng một thay thế (ký tự/chuỗi). Mẫu có thể là một ký tự hoặc một chuỗi, hoặc regexp.replace() method replaces some or all occurrences of a pattern with a replacement(character/string). The pattern can be a character or a string, or
regExp. Syntax:- Example:- Quảng cáo Thay thế tất cả các lần xuất hiện của tất cả các ký tự đặc biệt bằng cách (trống) từ chuỗi JavaScript là @ a # ngôn ngữ, đây là: ngôn ngữ phổ biến nhất. (empty) from the string “Javascript is @ a # language, This is : the most %popular _ language.” let newString = "" let dummyString = "Javascript is @ a # language, This is : the most %popular _ language." newString = dummyString.replace(/[`[email protected]#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '') console.log(dummyString) console.log(newString) Output:- Javascript is @ a # language, This is : the most %popular _ language. Javascript is a language This is the most popular language Ở đây, trong hàm thay thế (), đối số đầu tiên lấy các ký tự mà chúng tôi muốn thay thế. Đối số thứ hai là ký tự thay thế. Lưu ý rằng & nbsp; ở đây & nbsp; ‘, '(dấu phẩy) và‘.' (Dot) & nbsp; cũng bị xóa. Trong trường hợp bạn muốn giữ lại, hãy sử dụng: here ‘,'(comma) and ‘.'(dot) are also removed. In case you want to retain, use: let newString = "" let dummyString = "Javascript is @ a # language, This is : the most %popular _ language." newString = dummyString.replace(/[`[email protected]#$%^&*()_|+\-=?;:'"<>\{\}\[\]\\\/]/gi, '') console.log(dummyString) console.log(newString) Output:- Javascript is @ a # language, This is : the most %popular _ language. Javascript is a language, This is the most popular language. Example:- Thay thế tất cả các lần xuất hiện của tất cả các ký tự đặc biệt bằng cách _ _ (nhấn mạnh) từ chuỗi JavaScript là @ a # ngôn ngữ, đây là: ngôn ngữ phổ biến nhất. (underscore) from the string “Javascript is @ a # language, This is : the most %popular _ language.” let dummyString = "Javascript is @ a # language, This is : the most %popular _ language." newString = dummyString.replace(/[^a-zA-Z0-9][\W]/gi,'_') console.log(dummyString) console.log(newString) Output:- Javascript is @ a # language, This is : the most %popular _ language. Javascript is_ a_ language_This is_ the most_popular _language. JavaScript Thay thế các ký tự đặc biệt trong chuỗi bằng cách sử dụng hàm tùy chỉnhJavaScript Thay thế các ký tự đặc biệt Regex trong chuỗi bằng cách sử dụng thay thế () Example:- JavaScript & nbsp; thay thế () & nbsp; phương thức thay thế một số hoặc tất cả các lần xuất hiện của một mẫu bằng một thay thế (ký tự/chuỗi). Mẫu có thể là một ký tự hoặc một chuỗi, hoặc regexp. (empty) but retain the ‘,’ (comma) and ‘.’ (dots) from the string “Javascript is @ a # language, This is : the most %popular _ language.” let dummyString = "Javascript is @ a # language, This is : the most %popular _language." function replaceSpecialCharacters(_string) { var lowerCase = _string.toLowerCase(); var upperCase = _string.toUpperCase(); var replacement = ""; for(var i=0; i |