Trong hướng dẫn này, bạn sẽ học cách thay thế dấu gạch ngang bằng dấu cách trong javascript. Các từ trong câu được ngăn cách với nhau bằng dấu cách. Bằng cách có một khoảng trắng, không chỉ các từ mà toàn bộ câu trở nên có nghĩa. Dấu gạch ngang [
replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
1] trong câu tiếng Anh thường được dùng sau mệnh đề độc lập. Dấu gạch ngang có thanh ngang dài so với dấu gạch nốiCó rất nhiều cách để thay thế dấu gạch ngang bằng dấu cách. Chúng tôi sẽ sử dụng cách tiếp cận đơn giản nhất liên quan đến việc sử dụng mẫu biểu thức chính quy cũng như phương pháp
replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
2. Phương thức replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
2 tìm kiếm chuỗi cho một giá trị cụ thể hoặc mẫu biểu thức chính quy và nó trả về một chuỗi mới với các giá trị được thay thếTrong ví dụ sau, chúng ta có một biến toàn cục chứa một chuỗi. Khi bấm vào nút, chúng ta sẽ thay dấu gạch ngang bằng dấu cách và hiển thị kết quả trên màn hình. Vui lòng xem qua ví dụ về mã và các bước được đưa ra bên dưới
Hàm này hoạt động theo cách đệ quy, có nghĩa là nó sẽ tìm kiếm tất cả các lần xuất hiện của tham số đầu tiên thay vì dừng lại sau một lần xuất hiện
Xem xét ví dụ sau
$str = "php replace space with dash";
$str = str_replace[" ", "-", $str];
echo $str; // php-replace-space-with-dash
Nếu bạn đang tạo một URL, bạn cũng có thể muốn chuyển đổi chuỗi thành chữ thường
Bạn có thể gọi hàm
replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
4 với chuỗi như hình bên dưới// 👇 convert string to lowercase before replace
$str = "PHP replace Space with Dash";
$str = strtolower[$str];
$str = str_replace[" ", "-", $str];
echo $str; // php-replace-space-with-dash
// 👇 shorten the code above
$str = "PHP replace Space with Dash";
$str = str_replace[" ", "-", strtolower[$str]];
echo $str; // php-replace-space-with-dash
Khi cần thay dấu gạch ngang bằng dấu cách, bạn chỉ cần đảo ngược 2 tham số đầu của hàm
replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
5$str = "php-replace-dash-with-space";
$str = str_replace["-", " ", $str];
echo $str; // php replace dash with space
Bạn có thể thấy một số người khuyên dùng hàm
replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
6 để thay dấu cách bằng dấu gạch ngangMặc dù bạn có thể làm như vậy nhưng hàm
replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
6 tiêu tốn nhiều bộ nhớ và thời gian hơn để thực hiện thao tác tương tựChỉ sử dụng
replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
6 khi bạn có nhiều dấu cách mà bạn muốn chuyển đổi thành một dấu gạch ngang như sau JavaScript cung cấp hai chức năng để thay thế một chuỗi bằng một chuỗi khác. Bài viết hôm nay sẽ hướng dẫn chúng ta cả hai hàm thay dấu cách [’ ‘] bằng dấu gạch ngang [’-’]
Sử dụng replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
9 để thay thế khoảng trắng bằng dấu gạch ngang trong JavaScript
replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
Kỹ thuật
replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
9 trả về một chuỗi mới với tất cả các kết quả khớp của một mẫu được thay thế bằng một chuỗi thay thếMẫu thường là một chuỗi hoặc một RegExp và do đó, sự thay thế có thể là một chuỗi hoặc một hàm phải được gọi cho mỗi lần so khớp
cú pháp
replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
1 hoặc mẫu là một đối tượng hoặc chữ có cờ chung. Các kết quả phù hợp được thay thế bằng const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
2 hoặc giá trị được trả về bởi hàm thay thế đã chỉ địnhMột RegExp không có cờ toàn cầu
const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
0 làm tăng TypeError. const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
1. const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
2 là một chuỗi nên được thay thế bằng const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
2Nó được coi là một chuỗi ký tự và không được hiểu là một biểu thức chính quy
const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
2 hoặc const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
5 là chuỗi thay thế chuỗi con đã chỉ định bằng tham số const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
1 hoặc const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
2 đã chỉ định. Một số mẫu thay thế đặc biệt được cho phépHàm
const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
8 hoặc const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
9 được gọi để tạo chuỗi con mới được sử dụng để thay thế các kết quả khớp với biểu thức chính quy hoặc chuỗi con đã chỉ địnhMột chuỗi mới được trả về dưới dạng đầu ra, với tất cả các kết quả khớp của một mẫu được thay thế bằng một chuỗi thay thế
Thông tin thêm về chức năng
// 👇 convert string to lowercase before replace
$str = "PHP replace Space with Dash";
$str = strtolower[$str];
$str = str_replace[" ", "-", $str];
echo $str; // php-replace-space-with-dash
// 👇 shorten the code above
$str = "PHP replace Space with Dash";
$str = str_replace[" ", "-", strtolower[$str]];
echo $str; // php-replace-space-with-dash
10 có thể được tìm thấy trong tài liệu nàyconst p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
Trong ví dụ trên, chúng tôi đã thay thế khoảng trắng bằng chuỗi và áp dụng '-' cho phần khai báo dưới dạng một chuỗi mới. Nếu bạn muốn thay thế một chuỗi phức tạp, bạn có thể sử dụng regex
Nó tự động tìm mẫu phù hợp và thay thế nó bằng hàm
// 👇 convert string to lowercase before replace
$str = "PHP replace Space with Dash";
$str = strtolower[$str];
$str = str_replace[" ", "-", $str];
echo $str; // php-replace-space-with-dash
// 👇 shorten the code above
$str = "PHP replace Space with Dash";
$str = str_replace[" ", "-", strtolower[$str]];
echo $str; // php-replace-space-with-dash
10 hoặc chuỗi thay thếđầu ra
// 👇 convert string to lowercase before replace
$str = "PHP replace Space with Dash";
$str = strtolower[$str];
$str = str_replace[" ", "-", $str];
echo $str; // php-replace-space-with-dash
// 👇 shorten the code above
$str = "PHP replace Space with Dash";
$str = str_replace[" ", "-", strtolower[$str]];
echo $str; // php-replace-space-with-dash
1Sử dụng // 👇 convert string to lowercase before replace
$str = "PHP replace Space with Dash";
$str = strtolower[$str];
$str = str_replace[" ", "-", $str];
echo $str; // php-replace-space-with-dash
// 👇 shorten the code above
$str = "PHP replace Space with Dash";
$str = str_replace[" ", "-", strtolower[$str]];
echo $str; // php-replace-space-with-dash
12 để thay thế khoảng trắng bằng dấu gạch ngang trong JavaScript
// 👇 convert string to lowercase before replace
$str = "PHP replace Space with Dash";
$str = strtolower[$str];
$str = str_replace[" ", "-", $str];
echo $str; // php-replace-space-with-dash
// 👇 shorten the code above
$str = "PHP replace Space with Dash";
$str = str_replace[" ", "-", strtolower[$str]];
echo $str; // php-replace-space-with-dash
Kỹ thuật
// 👇 convert string to lowercase before replace
$str = "PHP replace Space with Dash";
$str = strtolower[$str];
$str = str_replace[" ", "-", $str];
echo $str; // php-replace-space-with-dash
// 👇 shorten the code above
$str = "PHP replace Space with Dash";
$str = str_replace[" ", "-", strtolower[$str]];
echo $str; // php-replace-space-with-dash
12 trả về một chuỗi mới với tất cả các kết quả khớp của một mẫu được thay thế bằng một chuỗi thay thếMẫu thường là một chuỗi hoặc một RegExp và do đó, sự thay thế có thể là một chuỗi hoặc một hàm phải được gọi cho mỗi lần so khớp
Nếu mẫu là chuỗi, nó sẽ chỉ thay thế lần xuất hiện khớp đầu tiên
cú pháp
// 👇 convert string to lowercase before replace
$str = "PHP replace Space with Dash";
$str = strtolower[$str];
$str = str_replace[" ", "-", $str];
echo $str; // php-replace-space-with-dash
// 👇 shorten the code above
$str = "PHP replace Space with Dash";
$str = str_replace[" ", "-", strtolower[$str]];
echo $str; // php-replace-space-with-dash
7const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
1 hoặc mẫu là một đối tượng hoặc chữ có cờ chung. Các kết quả phù hợp được thay thế bằng const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
2 hoặc giá trị được trả về bởi hàm thay thế đã chỉ địnhMột RegExp không có cờ toàn cầu
const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
0 làm tăng TypeError. // 👇 convert string to lowercase before replace
$str = "PHP replace Space with Dash";
$str = strtolower[$str];
$str = str_replace[" ", "-", $str];
echo $str; // php-replace-space-with-dash
// 👇 shorten the code above
$str = "PHP replace Space with Dash";
$str = str_replace[" ", "-", strtolower[$str]];
echo $str; // php-replace-space-with-dash
17. const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
2 là một chuỗi nên được thay thế bằng const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
2Nó được coi là một chuỗi ký tự và không được hiểu là một biểu thức chính quy
const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
2 hoặc const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
5 là chuỗi thay thế chuỗi con đã chỉ định bằng tham số const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
1 hoặc const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
2 đã chỉ định. Một số mẫu thay thế đặc biệt được cho phépHàm
const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
8 hoặc const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
9 được gọi để tạo chuỗi con mới được sử dụng để thay thế các kết quả khớp với biểu thức chính quy hoặc chuỗi con đã chỉ địnhMột chuỗi mới được trả về dưới dạng đầu ra, với tất cả các kết quả khớp của một mẫu được thay thế bằng một chuỗi thay thế
Thông tin thêm về chức năng
const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
5 có thể được tìm thấy trong tài liệu này$str = "php-replace-dash-with-space";
$str = str_replace["-", " ", $str];
echo $str; // php replace dash with space
6Trong ví dụ trên, chúng tôi đã thay thế khoảng trắng bằng chuỗi và áp dụng '-' cho phần khai báo dưới dạng một chuỗi mới. Nếu bạn muốn thay thế một chuỗi phức tạp, bạn có thể sử dụng regex
Nó tự động tìm mẫu phù hợp và thay thế nó bằng hàm
const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
5 hoặc chuỗi thay thếđầu ra
$str = "php-replace-dash-with-space";
$str = str_replace["-", " ", $str];
echo $str; // php replace dash with space
8Sự khác biệt duy nhất giữa
const p = 'Hello World! Welcome to my blog post.';
console.log[p.replaceAll[' ', '-']];
const regex = /\s/ig;
console.log[p.replaceAll[regex, '-']];
5 và // 👇 convert string to lowercase before replace
$str = "PHP replace Space with Dash";
$str = strtolower[$str];
$str = str_replace[" ", "-", $str];
echo $str; // php-replace-space-with-dash
// 👇 shorten the code above
$str = "PHP replace Space with Dash";
$str = str_replace[" ", "-", strtolower[$str]];
echo $str; // php-replace-space-with-dash
10 là nếu đối số tìm kiếm là một chuỗi, thì replaceAll[regexp, newSubstr]
replaceAll[regexp, replacerFunction]
replaceAll[substr, newSubstr]
replaceAll[substr, replacerFunction]
9 sẽ thay thế tất cả các lần tìm kiếm bằng giá trị hoặc hàm thay thế