给定一串文本
var string1 = 'IAmNotFoo';
你如何只提取大写字母?
'IANF'
以下是每个链接的一些方法:
function isUpperCase1(aCharacter) {
if ( ch == ch.toUpperCase() ) {
return true;
}
return false;
}
function isUpperCase2( aCharacter ) {
return ( aCharacter >= 'A' ) && ( aCharacter <= 'Z' );
}
var string1 = 'IAmNotFoo',
string2 = '',
i = 0,
ch = '';
while ( i <= string1.length ) {
ch = string1.charAt( i );
if (!isNaN( ch * 1 ) ) {
alert('character is numeric');
}
else if ( isUpperCase2() ) { // or isUpperCase1
string2 += ch;
}
i++;
}
或者简单地说(根据下面的评论):
var upper = str.replace(/[^A-Z]/g, '');
SO相关