JavaScript Regular Expression to Replace Special Characters from String
JavaScript Regular Expression to Replace Escape Special Characters from String
Enter Your Input Text
____1/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
Đôi khi, chúng tôi có các ký tự đặc biệt trong chuỗi mà chúng tôi không muốn hiển thị. Chúng tôi sử dụng phương thức
// Add your javascript here
$[function[]{
$["h1"].animate[{
"margin-left": "50px"
}, "slow"];
}];
3 để tìm kiếm một biểu thức chính quy [còn được gọi là biểu thức chính quy] hoặc một giá trịNó xuất ra một chuỗi mới với giá trị được thay thế nhưng không thay đổi chuỗi ban đầu. Hãy bắt đầu với cái cơ bản
Xóa các ký tự đặc biệt trong JavaScript mà không cần jQuery
Mã JavaScript
var stringValue = '&485,431,0458,92347';
var newString=stringValue.replace[/[^\&]|,/g, ' '];
console.log["String before replacement: " + stringValue];
console.log["String after replacement: " + newString];
đầu ra
"String before replacement: &485,431,0458,92347"
"String after replacement: 485 431 0458 92347"
Trong đoạn mã trên,
// Add your javascript here
$[function[]{
$["h1"].animate[{
"margin-left": "50px"
}, "slow"];
}];
4 xóa ký hiệu // Add your javascript here
$[function[]{
$["h1"].animate[{
"margin-left": "50px"
}, "slow"];
}];
5 và // Add your javascript here
$[function[]{
$["h1"].animate[{
"margin-left": "50px"
}, "slow"];
}];
6 [dấu phẩy] để xóa // Add your javascript here
$[function[]{
$["h1"].animate[{
"margin-left": "50px"
}, "slow"];
}];
6 khỏi chuỗi. Công cụ sửa đổi "String before replacement: &485,431,0458,92347"
"String after replacement: 485 431 0458 92347"
1 cho biết toàn cầu và "String before replacement: &485,431,0458,92347"
"String after replacement: 485 431 0458 92347"
2 được sử dụng làm toán tử "String before replacement: &485,431,0458,92347"
"String after replacement: 485 431 0458 92347"
3Nó có nghĩa là hàm
// Add your javascript here
$[function[]{
$["h1"].animate[{
"margin-left": "50px"
}, "slow"];
}];
3 lấy một biểu thức chính quy loại bỏ // Add your javascript here
$[function[]{
$["h1"].animate[{
"margin-left": "50px"
}, "slow"];
}];
5 và // Add your javascript here
$[function[]{
$["h1"].animate[{
"margin-left": "50px"
}, "slow"];
}];
6 khỏi toàn bộ chuỗi và thay thế nó bằng một khoảng trắngChúng ta cũng có thể định nghĩa một chuỗi chứa tất cả các ký tự đặc biệt và sử dụng chuỗi đó trong đối tượng
"String before replacement: &485,431,0458,92347"
"String after replacement: 485 431 0458 92347"
7 để làm cho mã dễ đọc và dễ bảo trì hơn. Xem đoạn mã sauvar specialChars = "!@#$^&%*[]+=-[]\/{}|:?,.";
var stringValue = '&485,431,[0458,]92347:';
console.log["String before replacement: " + stringValue];
for [var i = 0; i < specialChars.length; i++] {
stringValue = stringValue.replace[new RegExp["\\" + specialChars[i], "g"], ""];
}
console.log["String after replacement: " + stringValue];
đầu ra
// Add your javascript here
$[function[]{
$["h1"].animate[{
"margin-left": "50px"
}, "slow"];
}];
0Chúng tôi lặp lại một chuỗi hoàn chỉnh để tìm kiếm từng ký tự đặc biệt và loại bỏ nó để có đầu ra ở trên. Ở đây, đối tượng
"String before replacement: &485,431,0458,92347"
"String after replacement: 485 431 0458 92347"
8 khớp với văn bản trong mẫuBạn có thể tìm thêm về nó ở đây
Giả sử chúng tôi lấy một chuỗi từ phần tử HTML, thay thế các ký tự đặc biệt và hiển thị lại trên màn hình của bạn. Hãy thực hành điều đó với jQuery
Phương thức thay thế [] trong JavaScript được sử dụng để thay thế tất cả các ký tự đặc biệt từ một chuỗi bằng _ [dấu gạch dưới] được mô tả bên dưới
- phương thức thay thế []. Phương thức này tìm kiếm một chuỗi cho một giá trị đã xác định hoặc một biểu thức chính quy và trả về một chuỗi mới với giá trị đã xác định đã thay thế. cú pháp
// Add your javascript here
$[function[]{
$["h1"].animate[{
"margin-left": "50px"
}, "slow"];
}];
2- Thông số
- tìm kiếmVal. Nó là tham số bắt buộc. Nó chỉ định giá trị hoặc biểu thức chính quy sẽ thay thế bằng giá trị mới
- giá trị mới. Nó là tham số bắt buộc. Nó chỉ định giá trị để thay thế giá trị tìm kiếm bằng
ví dụ 1. Ví dụ này thay thế tất cả các ký tự đặc biệt bằng _ [gạch dưới] bằng cách sử dụng phương thức replace[].
html
/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
1/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
2____23/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
4/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
5/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
2/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
7/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
4/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
9/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
2____211/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
4/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
13/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
14/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
13/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
16/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
9/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
18____211/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
4/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
5/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
18/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
7/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
4/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
5/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
5/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
2/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
28 /* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
29/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
5/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
9/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
2____233 /* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
34/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
13/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
36/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
9/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
18____233/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
4/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
9/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
9/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
2____244 /* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
280 /* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
216/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
282 /* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
258 /* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
216/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
285/* Put your css in here */
h1 {
color: Green;
}
.success {
background-color: #57A957;
color: white;
background-repeat: no-repeat;
display: inline-block;
height: 40px;
vertical-align: middle;
}
.text {
height: 36px;
position: relative;
border: 1px solid #cdcdcd;
font-size: 14px;
width:300px;
}
4