Để trích xuất một số từ một chuỗi trong JavaScript, hãy gọi phương thức
const str = 'The number 345 has three digits';// No 'g' flag in regex6 trên chuỗi bằng biểu thức chính quy để thay thế tất cả các ký tự không phải chữ số trong chuỗi gốc. Ví dụ
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';const replaced = str.replace[/\D/g, ''];
console.log[replaced]; // 345
Phương thức
const str = 'The number 345 has three digits';// No 'g' flag in regex7
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex6 trả về một chuỗi mới với các kết quả khớp của một mẫu được thay thế bằng một chuỗi thay thế. Chúng tôi chuyển một biểu thức chính quy khớp với tất cả các ký tự không phải chữ số để chúng tôi có thể thay thế chúng bằng một chuỗi trống [
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex9] để xóa chúng
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Siêu ký tự regex
const str = 'The number 345 has three digits';// No 'g' flag in regex0 khớp với bất kỳ ký tự không phải chữ số nào trong một chuỗi
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Cờ
const str = 'The number 345 has three digits';// No 'g' flag in regex0 [toàn cầu] chỉ định rằng mọi lần xuất hiện của một ký tự không có chữ số trong chuỗi phải được khớp với biểu thức chính quy
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Nếu chúng ta không vượt qua cờ toàn cầu, chỉ ký tự không phải chữ số đầu tiên trong chuỗi đầu vào sẽ được khớp và thay thế
const str = 'The number 345 has three digits';// No 'g' flag in regex2. Phương thức khớp chuỗi[]
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Để trích xuất một số từ một chuỗi, chúng ta cũng có thể gọi phương thức
const str = 'The number 345 has three digits';// No 'g' flag in regex1 trên chuỗi, chuyển một biểu thức chính quy khớp với một chuỗi các chữ số liên tiếp. Ví dụ
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';const matches = str.match[/\d+/];
const numStr = matches[0];console.log[numStr]; // 345
Phương thức
const str = 'The number 345 has three digits';// No 'g' flag in regex7
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex1 so khớp một chuỗi với một biểu thức chính quy và trả về kết quả. Trong trường hợp của chúng tôi, số phù hợp là mục đầu tiên của mảng, vì vậy chúng tôi truy cập thuộc tính
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex4 với ký hiệu ngoặc để lấy nó
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Siêu ký tự
const str = 'The number 345 has three digits';// No 'g' flag in regex5 được sử dụng để tìm một chữ số trong một chuỗi. Chúng tôi thêm
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex6 vào
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex5 để tìm một chuỗi các chữ số liên tiếp
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Phương pháp thứ hai này tốt hơn khi cố gắng trích xuất từng số trong chuỗi một cách riêng biệt, vì nó coi một chuỗi các chữ số liên tiếp là một kết quả khớp riêng biệt. Để trích xuất riêng từng số, chúng tôi sẽ cần thêm cờ
const str = 'The number 345 has three digits';// No 'g' flag in regex0
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Có được những hiểu biết hữu ích và nâng cao kiến thức phát triển web của bạn với các mẹo và hướng dẫn hàng tuần từ Coding Beauty. Hơn 1.400 nhà phát triển đăng ký
1. Phương thức thay thế chuỗi []
Để trích xuất một số từ một chuỗi trong JavaScript, hãy gọi phương thức
const str = 'The number 345 has three digits';// No 'g' flag in regex6 trên chuỗi bằng biểu thức chính quy để thay thế tất cả các ký tự không phải chữ số trong chuỗi gốc. Ví dụ
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex1
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Phương thức
const str = 'The number 345 has three digits';// No 'g' flag in regex7
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex6 trả về một chuỗi mới với các kết quả khớp của một mẫu được thay thế bằng một chuỗi thay thế. Chúng tôi chuyển một biểu thức chính quy khớp với tất cả các ký tự không phải chữ số để chúng tôi có thể thay thế chúng bằng một chuỗi trống [
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex9] để xóa chúng
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Siêu ký tự regex
const str = 'The number 345 has three digits';// No 'g' flag in regex0 khớp với bất kỳ ký tự không phải chữ số nào trong một chuỗi
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Cờ
const str = 'The number 345 has three digits';// No 'g' flag in regex20 [toàn cầu] chỉ định rằng mọi lần xuất hiện của một ký tự không phải chữ số trong chuỗi phải được khớp với biểu thức chính quy
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Nếu chúng ta không vượt qua cờ toàn cầu, chỉ ký tự không phải chữ số đầu tiên trong chuỗi đầu vào sẽ được khớp và thay thế
const str = 'The number 345 has three digits';// No 'g' flag in regex2
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
2. Phương thức khớp chuỗi[]
Để trích xuất một số từ một chuỗi, chúng ta cũng có thể gọi phương thức
const str = 'The number 345 has three digits';// No 'g' flag in regex21 trên chuỗi, chuyển một biểu thức chính quy khớp với một chuỗi các chữ số liên tiếp. Ví dụ
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex5
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Phương thức
const str = 'The number 345 has three digits';// No 'g' flag in regex7
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex21 so khớp chuỗi với biểu thức chính quy và trả về kết quả. Trong trường hợp của chúng tôi, số phù hợp là mục đầu tiên của mảng, vì vậy chúng tôi truy cập thuộc tính
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex24 với ký hiệu ngoặc để lấy nó
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Siêu ký tự
const str = 'The number 345 has three digits';// No 'g' flag in regex25 được sử dụng để tìm một chữ số trong một chuỗi. Chúng tôi thêm
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex26 vào
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex25 để tìm một chuỗi các chữ số liên tiếp
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Phương pháp thứ hai này tốt hơn khi cố gắng trích xuất từng số trong chuỗi một cách riêng biệt, vì nó coi một chuỗi các chữ số liên tiếp là một kết quả khớp riêng biệt. Để trích xuất riêng từng số, chúng tôi sẽ cần thêm cờ
const str = 'The number 345 has three digits';// No 'g' flag in regex20
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex2
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
Ghi chú
Khi không tìm thấy chữ số nào, phương thức
const str = 'The number 345 has three digits';// No 'g' flag in regex21 sẽ trả về
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex50
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
const str = 'The number 345 has three digits';// No 'g' flag in regex5
const replaced = str.replace[/\D/, ''];console.log[replaced]; // he number 345 has three digits
11 Tính năng JavaScript mới tuyệt vời trong ES13
Hướng dẫn này sẽ giúp bạn cập nhật tất cả các tính năng mới nhất được thêm vào trong ECMAScript 13. Những tính năng mới mạnh mẽ này sẽ hiện đại hóa JavaScript của bạn bằng mã ngắn hơn và biểu cảm hơn
Đăng ký và nhận ngay một bản sao miễn phí
Ayibatari Ibaba
Ayibatari Ibaba là nhà phát triển phần mềm có nhiều năm kinh nghiệm xây dựng trang web và ứng dụng. Anh ấy đã viết rất nhiều về nhiều chủ đề lập trình và đã tạo ra hàng chục ứng dụng và thư viện mã nguồn mở