[英]Manipulating occurrences of all letters in a string in Javascript
Desired functionality: 所需功能:
var myString = "123a4 1b234";
console.log(myString.allInstancesOfLetter().something();
//"123A4 1B234"
Is there a way to do this without having to complicate things with an array of all 26 letters etc.? 有没有一种方法可以不必将所有26个字母等组成的数组复杂化呢?
edit: toUpperCase() causing confusion, I didn't really think of that interaction, I was just choosing a random function. 编辑:toUpperCase()引起混乱,我并没有真正想到这种相互作用,我只是选择一个随机函数。
You can set a callback function as second parameter when using the replace
method that will be applied to each ocurrence in a set RegExp. 在使用将应用于设置的RegExp中的每个事件的
replace
方法时,可以将回调函数设置为第二个参数。
const myString = "123a4 1b234"; const result = myString.replace(/[az]/g, function(char) { return char.toUpperCase(); }); console.log(result);
toUpperCase
isnt applied to numbers, so toUpperCase
不适用于数字,因此
"123a4 1b234".toUpperCase()
works as expected. 如预期般运作。
You just need String.prototype.toUpperCase()
您只需要
String.prototype.toUpperCase()
var myString = "123a4 1b234"; console.log(myString.toUpperCase());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.