如何在JavaScript中将所有大写字母移动到字符串的开头?
假设以下是我们的字符串-
my name is JOHN SMITH
使用sort()正则表达式沿着/[AZ]/所有大写字母移动到字符串的开头/
示例
var moveAllCapitalLettersAtTheBeginning = [...' my name is JOHN SMITH ']
.sort((value1, value2) =>
/[A-Z]/.test(value1) ? /[A-Z]/.test(value2) ? 0 : -1 : 0).join(' ');
console.log("After moving the all capital letters at the beginning=");
console.log(moveAllCapitalLettersAtTheBeginning);要运行上述程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo199.js。
输出结果
这将产生以下输出-
PS C:\Users\Amit\javascript-code> node demo199.js After moving the all capital letters at the beginning= J O H N S M I T H m y n a m e i s
热门推荐
10 元旦的祝福语简短 公司
11 公司开年仪式祝福语简短
12 简短的祝福语12字
13 到同事新家简短祝福语
14 圣诞祝福语简短文字
15 月子小孩祝福语大全简短
16 生日宝宝祝福语简短独特
17 女朋友考试祝福语简短
18 很暧昧的简短祝福语