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';

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 //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] 

Chủ Đề