Hướng dẫn get only string from string javascript - chỉ lấy chuỗi từ chuỗi javascript

Tôi có một chuỗi "5a" hoặc "A6". Tôi chỉ muốn nhận được "A" hoặc "A" trên kết quả. Tôi đang sử dụng những điều sau đây nhưng nó không hoạt động.

JavaScript

 var answer = '5A';
 answer = answer.replace(/^[0-9]+$/i);
 //console.log(answer) should be 'A';

Hướng dẫn get only string from string javascript - chỉ lấy chuỗi từ chuỗi javascript

PSL

123K21 Huy hiệu vàng252 Huy hiệu bạc242 Huy hiệu Đồng21 gold badges252 silver badges242 bronze badges

Hỏi ngày 4 tháng 9 năm 2013 lúc 21:55Sep 4, 2013 at 21:55

0

let answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiple occasions
// the flag `i` is in case sensitive which means that `A` and `a` is treated as the same character ( and `B,b`, `C,c` etc )  

Thay vì

let answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiple occasions
// the flag `i` is in case sensitive which means that `A` and `a` is treated as the same character ( and `B,b`, `C,c` etc )  
3 thì bạn có thể sử dụng công cụ sửa đổi
let answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiple occasions
// the flag `i` is in case sensitive which means that `A` and `a` is treated as the same character ( and `B,b`, `C,c` etc )  
4 và
let answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiple occasions
// the flag `i` is in case sensitive which means that `A` and `a` is treated as the same character ( and `B,b`, `C,c` etc )  
5 sẽ phù hợp với bất kỳ chữ cái nào, và không chỉ là Z mặc dù:

'50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
// [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
// the flag `g` means it should match multiple occasions
// the flag `u` will enable the support for unicode character classes. 

Xem https://caniuse.com/mdn-javascript_builtins_regexp_unicode để được hỗ trợ

Đã trả lời ngày 4 tháng 9 năm 2013 lúc 22:04Sep 4, 2013 at 22:04

Andreas Louvandreas LouvAndreas Louv

45.1K13 Huy hiệu vàng97 Huy hiệu bạc118 Huy hiệu đồng13 gold badges97 silver badges118 bronze badges

1

 var answer = '5A';
 answer = answer.replace(/[^A-Za-z]/g, '');

let answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiple occasions
// the flag `i` is in case sensitive which means that `A` and `a` is treated as the same character ( and `B,b`, `C,c` etc )  
6 cho toàn cầu, không
let answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiple occasions
// the flag `i` is in case sensitive which means that `A` and `a` is treated as the same character ( and `B,b`, `C,c` etc )  
7 hoặc
let answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiple occasions
// the flag `i` is in case sensitive which means that `A` and `a` is treated as the same character ( and `B,b`, `C,c` etc )  
8 và
let answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiple occasions
// the flag `i` is in case sensitive which means that `A` and `a` is treated as the same character ( and `B,b`, `C,c` etc )  
9 để thay thế nó bằng không có gì. Để lại tham số thứ hai thay thế nó bằng chuỗi
'50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
// [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
// the flag `g` means it should match multiple occasions
// the flag `u` will enable the support for unicode character classes. 
0.

Tôi tự hỏi nếu một cái gì đó như thế này có thể nhanh hơn, nhưng nó và biến thể chậm hơn nhiều:

function alphaOnly(a) {
    var b = '';
    for (var i = 0; i < a.length; i++) {
        if (a[i] >= 'A' && a[i] <= 'z') b += a[i];
    }
    return b;
}

http://jsperf.com/strip-non-alpha

Đã trả lời ngày 4 tháng 9 năm 2013 lúc 21:57Sep 4, 2013 at 21:57

Trevor Dixontrevor DixonTrevor Dixon

21K11 Huy hiệu vàng69 Huy hiệu bạc103 Huy hiệu đồng11 gold badges69 silver badges103 bronze badges

1

Cách bạn hỏi, bạn muốn tìm chữ cái thay vì xóa số (tương tự trong ví dụ này, nhưng có thể khác nhau tùy thuộc vào hoàn cảnh của bạn) - nếu đó là những gì bạn muốn, có một con đường khác mà bạn có thể chọn:

var answer = "5A";
var match = answer.match(/[a-zA-Z]/);
answer = match ? match[0] : null;

Nó tìm kiếm một trận đấu trên chữ cái, thay vào đó loại bỏ số. Nếu một trận đấu được tìm thấy, thì

'50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
// [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
// the flag `g` means it should match multiple occasions
// the flag `u` will enable the support for unicode character classes. 
1 sẽ đại diện cho chữ cái đầu tiên, nếu không
'50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
// [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
// the flag `g` means it should match multiple occasions
// the flag `u` will enable the support for unicode character classes. 
2 sẽ là NULL.

Đã trả lời ngày 4 tháng 9 năm 2013 lúc 22:05Sep 4, 2013 at 22:05

Hướng dẫn get only string from string javascript - chỉ lấy chuỗi từ chuỗi javascript

Joe Enosjoe EnosJoe Enos

Huy hiệu vàng 38.6K1178 Huy hiệu bạc130 Huy hiệu đồng11 gold badges78 silver badges130 bronze badges

var answer = '5A';
answer = answer.replace(/[0-9]/g, '');

Bạn cũng có thể làm điều đó mà không có biểu thức thông thường nếu bạn quan tâm đến hiệu suất;)

Bạn mã hóa nhiều vấn đề:

  • '50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
    // [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
    // the flag `g` means it should match multiple occasions
    // the flag `u` will enable the support for unicode character classes. 
    
    3 Lấy các tham số kéo: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/string/replace
  • Cờ
    '50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
    // [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
    // the flag `g` means it should match multiple occasions
    // the flag `u` will enable the support for unicode character classes. 
    
    4, đứng cho
    '50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
    // [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
    // the flag `g` means it should match multiple occasions
    // the flag `u` will enable the support for unicode character classes. 
    
    5, không có ý nghĩa gì vì bạn đang xử lý các con số (trường hợp trên 1 là gì ?!)
  • '50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
    // [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
    // the flag `g` means it should match multiple occasions
    // the flag `u` will enable the support for unicode character classes. 
    
    6 sẽ chỉ khớp với một số, không có gì hơn. Bạn nên kiểm tra điều này: http://www.regexper.com/. Nhập Regex của bạn (không có dấu gạch chéo) vào hộp và nhấn Enter!

Nói chung, tôi sẽ khuyên bạn nên tìm hiểu một chút về các biểu thức cơ bản thông thường. Dưới đây là một ứng dụng hữu ích để chơi với họ: http://rubular.com/

Trevor Dixon

21K11 Huy hiệu vàng69 Huy hiệu bạc103 Huy hiệu đồng11 gold badges69 silver badges103 bronze badges

Cách bạn hỏi, bạn muốn tìm chữ cái thay vì xóa số (tương tự trong ví dụ này, nhưng có thể khác nhau tùy thuộc vào hoàn cảnh của bạn) - nếu đó là những gì bạn muốn, có một con đường khác mà bạn có thể chọn:Sep 4, 2013 at 21:58

Nó tìm kiếm một trận đấu trên chữ cái, thay vào đó loại bỏ số. Nếu một trận đấu được tìm thấy, thì

'50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
// [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
// the flag `g` means it should match multiple occasions
// the flag `u` will enable the support for unicode character classes. 
1 sẽ đại diện cho chữ cái đầu tiên, nếu không
'50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
// [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
// the flag `g` means it should match multiple occasions
// the flag `u` will enable the support for unicode character classes. 
2 sẽ là NULL.Aegis

Đã trả lời ngày 4 tháng 9 năm 2013 lúc 22:0511 silver badges12 bronze badges

5

Joe Enosjoe Enos It will extract all Alphabets from any string..

     var answer = '5A';
         answer = answer.replace(/[^a-zA-Z]/g, '');

/*var answer = '5A';
     answer = answer.replace(/[^a-zA-Z]/g, '');*/
$("#check").click(function(){
$("#extdata").html("Extraxted Alphabets : "+$("#data").val().replace(/[^a-zA-Z]/g, '')+"");
});
i{
color:green;
  letter-spacing:1px;
}
let answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiple occasions
// the flag `i` is in case sensitive which means that `A` and `a` is treated as the same character ( and `B,b`, `C,c` etc )  
0

Huy hiệu vàng 38.6K1178 Huy hiệu bạc130 Huy hiệu đồngSep 3, 2016 at 19:07

Hướng dẫn get only string from string javascript - chỉ lấy chuỗi từ chuỗi javascript

1

Bạn cũng có thể làm điều đó mà không có biểu thức thông thường nếu bạn quan tâm đến hiệu suất;)

let answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiple occasions
// the flag `i` is in case sensitive which means that `A` and `a` is treated as the same character ( and `B,b`, `C,c` etc )  
1

Bạn mã hóa nhiều vấn đề:Sep 4, 2013 at 22:03

'50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
// [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
// the flag `g` means it should match multiple occasions
// the flag `u` will enable the support for unicode character classes. 
3 Lấy các tham số kéo: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/string/replaceOriol

Cờ

'50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
// [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
// the flag `g` means it should match multiple occasions
// the flag `u` will enable the support for unicode character classes. 
4, đứng cho
'50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
// [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
// the flag `g` means it should match multiple occasions
// the flag `u` will enable the support for unicode character classes. 
5, không có ý nghĩa gì vì bạn đang xử lý các con số (trường hợp trên 1 là gì ?!)57 gold badges412 silver badges494 bronze badges

let answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiple occasions
// the flag `i` is in case sensitive which means that `A` and `a` is treated as the same character ( and `B,b`, `C,c` etc )  
2

'50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
// [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
// the flag `g` means it should match multiple occasions
// the flag `u` will enable the support for unicode character classes. 
6 sẽ chỉ khớp với một số, không có gì hơn. Bạn nên kiểm tra điều này: http://www.regexper.com/. Nhập Regex của bạn (không có dấu gạch chéo) vào hộp và nhấn Enter!Oct 6 at 18:26

1

Làm thế nào để bạn trích xuất một chuỗi từ một chuỗi?

Bạn gọi phương thức Subring (Int32) để trích xuất một chuỗi con từ một chuỗi bắt đầu ở một vị trí ký tự được chỉ định và kết thúc ở cuối chuỗi. Vị trí ký tự bắt đầu là dựa trên không; Nói cách khác, ký tự đầu tiên trong chuỗi là tại INDEX 0, không phải chỉ mục 1.call the Substring(Int32) method to extract a substring from a string that begins at a specified character position and ends at the end of the string. The starting character position is zero-based; in other words, the first character in the string is at index 0, not index 1.

Làm thế nào để bạn có được một phần của một chuỗi?

Phương thức chuỗi con () trích xuất một phần của chuỗi.Phương thức Subr () bắt đầu ở một vị trí được chỉ định và trả về một số ký tự được chỉ định.Phương thức Subr () không thay đổi chuỗi gốc.Để trích xuất các ký tự từ cuối chuỗi, hãy sử dụng vị trí bắt đầu âm.. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position.

Làm cách nào để tìm kiếm một chuỗi số?

Hãy thử chức năng đơn giản này sẽ trả về các số từ một chuỗi:..
Chuỗi riêng getnumbers (Chuỗi InputString).
Chuỗi kết quả = "";..
Chuỗi số = "0123456789" ;.
int i = 0 ;.
for (i = 0; i
if (số. chứa (InputString. Elementat (i))).

Sự khác biệt giữa chuỗi con và chuỗi con là gì?

Sự khác biệt giữa chuỗi con () và chuỗi con (), hai tham số của chuỗi con () là bắt đầu và độ dài, trong khi đối với chuỗi con (), chúng là bắt đầu và kết thúc.Chỉ số bắt đầu của Subr () sẽ kết thúc đến cuối chuỗi nếu nó âm, trong khi Subring () sẽ kẹp nó thành 0.The two parameters of substr() are start and length , while for substring() , they are start and end . substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 .