jQuery 如何从字母数字字符串中仅提取字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18624457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I extract only alphabet from a alphanumeric string
提问by rex
I have a string "5A" or "a6". I want to get only "A" or "a" on the result. I am using the following but it's not working.
我有一个字符串“5A”或“a6”。我只想在结果上得到“A”或“a”。我正在使用以下内容,但它不起作用。
Javascript
Javascript
var answer = '5A';
answer = answer.replace(/^[0-9]+$/i);
//console.log(answer) should be 'A';
回答by andlrc
var answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiply 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 )
回答by Trevor Dixon
var answer = '5A';
answer = answer.replace(/[0-9]/g, '');
g
for global, no ^
or $
, and ''
to replace it with nothing. Leaving off the second parameter replaces it with the string 'undefined'
.
g
对于 global, no ^
or $
,并''
用空替换它。不使用第二个参数会将其替换为 string 'undefined'
。
I wondered if something like this this might be faster, but it and variations are much slower:
我想知道这样的事情是否可能更快,但它和变化要慢得多:
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;
}
回答by Joe Enos
The way you asked, you want to find the letter rather than remove the number (same thing in this example, but could be different depending on your circumstances) - if that's what you want, there's a different path you can choose:
你问的方式,你想找到字母而不是删除数字(在这个例子中是相同的,但根据你的情况可能会有所不同) - 如果这是你想要的,你可以选择不同的路径:
var answer = "5A";
var match = answer.match(/[a-zA-Z]/);
answer = match ? match[0] : null;
It looks for a match on the letter, rather that removing the number. If a match is found, then match[0]
will represent the first letter, otherwise match
will be null.
它寻找字母上的匹配项,而不是删除数字。如果找到匹配项,则match[0]
代表第一个字母,否则match
为空。
回答by Aegis
var answer = '5A';
answer = answer.replace(/[0-9]/g, '');
You can also do it without a regular expression if you care about performance ;)
如果您关心性能,也可以在没有正则表达式的情况下进行 ;)
You code hade multiple issues:
您的代码有多个问题:
string.replace
takes tow parameters: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace- the flag
i
, standing forcase insensitive
, doesn't make sense since you are dealing with numbers (what's an upper-case 1?!) /^[0-9]+$/
would only match a number, nothing more. You should check this out: http://www.regexper.com/. Enter your regex (without the slashes) in the box and hit enter!
string.replace
需要两个参数:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace- 国旗
i
,站立case insensitive
,不会因为你是在处理数字意义(什么是大写的1?!) /^[0-9]+$/
只会匹配一个数字,仅此而已。你应该看看这个:http: //www.regexper.com/。在框中输入您的正则表达式(不带斜杠),然后按回车键!
In general I would advice you to learn a bit about basic regular expressions. Here is a useful app to play with them: http://rubular.com/
一般来说,我建议你学习一些基本的正则表达式。这是一个与他们一起玩的有用的应用程序:http: //rubular.com/
回答by Oriol
You can simplify a bit @TrevorDixon's and @Aegis's answers using \d
(digit) instead of [0-9]
您可以使用\d
(digit) 代替@TrevorDixon 和 @Aegis 的答案来简化一点[0-9]
var answer = '5A';
answer = answer.replace(/\d/g, '');
回答by Vishal Singh
JavaScript:It will extract all Alphabets from any string..
JavaScript:它将从任何字符串中提取所有字母。
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 : <i >"+$("#data").val().replace(/[^a-zA-Z]/g, '')+"</i>");
});
i{
color:green;
letter-spacing:1px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="text" id="data">
<button id="check">Click To Extract</button><br/>
<h5 id="extdata"></h5>
</div>