Thay thế dấu gạch ngang bằng dấu cách javascript

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ối

Có 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 ngang

Mặ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

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ỉ định

Mộ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, '-']];
2

Nó đượ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ép

Hà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ỉ định

Mộ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ày

const 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
1

Sử 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

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
7

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ỉ định

Mộ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, '-']];
2

Nó đượ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ép

Hà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ỉ định

Mộ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
6

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

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
8

Sự 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ế

Làm cách nào để thay thế dấu gạch ngang trong JavaScript?

Sử dụng phương thức replaceAll[] để thay thế dấu cách bằng dấu gạch ngang trong một chuỗi , e. g. str. thay thế tất cả[' ', '-']. Phương thức replaceAll sẽ trả về một chuỗi mới trong đó tất cả khoảng trắng được thay thế bằng dấu gạch ngang.

Làm cách nào để thay thế tất cả khoảng trắng bằng dấu gạch nối trong JavaScript?

Chuỗi là bất biến. String replaceText = "AT AT"; . thay thế tất cả[" ", "-"]; . String replaceText = "AT AT"; . replace[Char, Char] is sufficient [and faster] for replacing all occurrences. String replaceText = "AT AT"; replaceText = replaceText.

Làm cách nào để xóa dấu gạch ngang trong JavaScript?

Sử dụng Chuỗi. phương thức thay thế [] để xóa tất cả dấu gạch nối khỏi chuỗi , e. g. const dấu gạch nốiRemoved = str. thay thế[/-/g, ''];. Phương thức thay thế [] sẽ xóa tất cả các dấu gạch ngang khỏi chuỗi bằng cách thay thế chúng bằng các chuỗi trống.

Làm cách nào để thay thế các ký tự bằng khoảng trắng trong Java?

Chương trình. .
lớp công cộng ReplaceSpace
public static void main[String[] args] {
Chuỗi string = "Trăng xanh một lần";
char ch = '-';
// Thay thế khoảng trắng bằng ký tự cụ thể ch
chuỗi = chuỗi. thay[' ', ch];
Hệ thống. ngoài. println["Chuỗi sau khi thay khoảng trắng bằng ký tự đã cho. "];

Chủ Đề