Reputation: 1297
I have a string like `"TransfCoolingFanG1" I want to have the output: "TCFG1". How I build a javascript function for this purpose?
Upvotes: 1
Views: 779
Reputation: 115212
Use String#replace
method and replace all small letters.
console.log(
"TransfCoolingFanG1".replace(/[a-z]+/g, '')
)
UPDATE : If you want to remove all character except capital case or digit then use negated character class with regex.
console.log(
"TransfCoolingFanG1".replace(/[^A-Z\d]+/g, '')
)
Upvotes: 1
Reputation: 11
EcmaScript 6 approach
inputString => inputString.split('').filter(x => x === x.toUpperCase()).join('')
So what happens here in this arrow function:
Upvotes: 1
Reputation: 1342
Maybe more native :
var yourString = "TransfCoolingFanG1";
var upString= "";
for (var i = 0; i < yourString.length;i++){
if (isUpperCase(yourString.charAt(i)));{
upString +=yourString.charAt(i);
}
}
window.alert(upString);
Upvotes: 1
Reputation: 8886
var str = "TransfCoolingFanG1";
var res = "";
var len = str.length;
for (var i = 0; i < len ; i++)
if (str[i] === str[i].toUpperCase())
res = res + str[i];
window.alert(res);
Upvotes: 1
Reputation: 11318
Or, another approach (replace all EXCEPT uppercase letters and numbers):
str="TransfCo^^^oli*****ngFanG1";
str=str.replace(/[^A-Z0-9]/g,'');
console.log(str);
Upvotes: 2